Gym memberships

A big gym network has several branches at different locations. They find it sometimes hard to keep track of the people that have their memberships. They know the name of the people and where their gym is, but they would like to know the list of people for each location.
The first line of the input contains a single integer n the number of people who have signed up for a gym membership. The next n lines contain " - " separated person's name and the location of the gym where they have their membership. The next line contains a single integer q, the number of location queries. The following q lines contain the names of the locations.
The program should print q lines. Each line should contain space-separated names of people who are attending the gyms at that location.
Bob - Green st. 1
Lucy - Park Dr. 25
Anna - Green st. 1
Lucas - Sharon Park Dr. 8
Simon - Sharon Park Dr. 8
Sharon Park Dr. 8
Green st. 1
Park Dr. 25

Lucas Simon
Bob Anna
Note: The names should be in the order of appearance in the input
To check your solution you need to sign in
Sign in to continue