I tried many combination of data structure but still got an time limit exceeded on test 7. At the end, I found that it’s about the way of printing.
What I did is instead of calling print every time in the for loop, I used a variable to store the answer and print it at once at the end.
However, note that it is not always faster when I time it in my code (Please share your thought if you have any idea!) but it could be a trick to improve performance in Codeforces.