Last 2 Test cases giving TLE

link: https://ide.codingblocks.com/s/265683
The last 2 test cases are giving TLE. I have even tried taking long long int.
My code seems to have order of O(n). What am I missing?

@rmukhopadhyay_be17 always use unordered_map when you need hashing and do need ordering and all cause it is more efficient compared to map with respect to time.