program erfenchazhao;
var
n,k,jilu,l:longint;
m:string;
a:array[1..10000]of string [100];
b:array[1..10000]of longint;
procedure qsort(l,r: longint);
var
i,j,y1: longint;
x,y2:string;
begin
i:=l;j:=r; x:=a[(l+r) div 2];
repeat
while a[i]<x do inc(i);
while x<a[j] do dec(j);
if i<=j then
begin
y2:=a[i];
a[i]:=a[j];
a[j]:=y2;
y1:=b[i];
b[i]:=b[j];
b[j]:=y1;
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
procedure chazhao(x1,y1:integer);
begin
if a[(x1+y1) div 2]=m then
begin
jilu:=(x1+y1) div 2;
exit
end;
if a[(x1+y1) div 2]>m then
begin
chazhao(x1,(x1+y1) div 2-1);
exit
end
else
begin
chazhao((x1+y1) div 2+1,y1);
exit
end
end;
begin
assign (input,'scanword.in');
reset (input);
assign (output,'scanword.out');
rewrite (output);
readln (n);
for k:=1 to n do
begin
readln (a[k]);
readln (b[k]);
end;
qsort (1,n);
readln (l);
for k:=1 to l do
begin
readln (m);
chazhao(1,n);
writeln (b[jilu])
end;
close (input);
close (output)
end.