eolymp
bolt
Try our new interface for solving problems
Problems

Java Collections Students Subjects

Java Collections Students Subjects

Each student can take many subjects. Each subject can be taken by many students. Simulate the process of taking the subject.

Input

First line contains number n. Each of the next n lines have the form: Subject Student's_Surname. It means that subject Subject was taken by student with given surname. Then given number of queries q. Each of the next q lines contains either subject or surname.

Output

If the query contains subject, print in one line in acceding order the surnames of the students who took this subject. If the query contains surname, print in one line in acceding order the subjects taken by this student.

Time limit 1 second
Memory limit 128 MiB
Input example #1
7
Mathematics Ivanov
Physics Petrov
Mathematics Sidorov
Mathematics Eliseev
Physics Egorov
Mathematics Petrov
Physics Ivanov
3
Mathematics
Petrov
Physics
Output example #1
Eliseev Ivanov Petrov Sidorov 
Mathematics Physics 
Egorov Ivanov Petrov 
Author Mykhailo Medvediev