var
y,z:array[0..10001] of string;
a,b,c,m,n:longint;
x:array[0..10001] of longint;
begin
assign(input,'scanword.in');
reset(input);
assign(output,'scanword.out');
rewrite(output);
readln(n);
for a:=1 to n do
begin
readln(y[a]);
readln(x[a]);
end;
readln(m);
for a:=1 to m do
readln(z[a]);
for a:=1 to n do
for b:=1 to n do
if y[a]=z[b] then writeln(x[a]);
close(input);
close(output);
end.