You are using both cin and scanf - so what effect does ios::sync_with_stdio(0) have on that? The answer is that n would get some very strange values, far larger than you might expect.
I know why you’re using tFinish_idx.erase(it), but you’re not answering the question - what may happen to the ITERATOR after using using it? See www.cplusplus.com/reference/map/multimap/erase/. Iterators, pointers and references referring to elements removed by the function are invalidated. The iterator is invalidated, but you still continue to use it.