Here is a link to problem 498B http://codeforces.net/contest/498/problem/B
I attempted the problem and analyzed my solution to run in O(nT), but my submission exceeds the time limit on test #65. I would be glad if someone could help me figure out why it is exceeding the time limit (or explained why my solution didn't run in O(nT)). http://codeforces.net/contest/498/submission/12259316