Trailer Tailor: Nearest Mood Match

A streaming app’s Trailer Tailor must pick a trailer vibe: funny or serious. It looks at how long people watch (minutes), how often they rewatch, what genre they love, and which device they use. But minutes and rewatches sit on different scales, and genres/devices are words - so the app needs a fair way to compare everything before choosing the closest existing example.

You are asked to read a vocabulary of genres and devices, an initial set of viewing records, and then classify new records with 1-NN. Use statistics from the initial records only to put the two numeric features on the same footing by standardizing them (with z-score), turn the two categorical features into one-hot encodings according to the given vocabularies, concatenate all features, and pick the single nearest neighbor by Euclidean distance. If several neighbors are equally near, print the alphabetically smallest label. Tokens not found in a vocabulary contribute no active bit for that field.

The first line of the input contains a single integer v1 followed by a line of v1 tokens forming the genre vocabulary. The next line contains a single integer v2 followed by a line of v2 tokens forming the device vocabulary.

The next line contains a single integer n representing how many initial records you have. Each of the next n lines contains two floating-point numbers and three tokens - watch_time rewatch_count genre device label.

The next line contains a single integer q. Each of the next q lines contains watch_time rewatch_count genre device for the records to classify.

The program should print q lines. Each line should contain the predicted label: funny or serious.

Input

Output

3
comedy action drama
2
mobile tv
5
30 5 comedy mobile funny
80 1 drama tv serious
60 2 action mobile serious
25 6 comedy tv funny
90 1 drama tv serious
2
28 5 comedy mobile
75 1 drama tv

funny
serious

2
comedy drama
3
mobile tv web
4
40 3 comedy mobile funny
70 1 drama web serious
50 4 comedy tv funny
85 1 drama tv serious
2
50 4 comedy tv
85 1 drama tv

funny
serious

3
comedy action drama
2
mobile tv
4
35 4 comedy mobile funny
70 1 drama tv serious
55 3 action tv serious
32 5 comedy tv funny
2
35 4 comedy tablet
60 2 action tv

funny
serious

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue