Program scanword;
Type
abc=record
k:string;
p:longint;
end;
sc=array [1..26,0..10000] of abc;
Var
s:sc;
z:string;
i,j,q:longint;
n,m:longint;
Begin
assign(input,'scanword.in');
assign(output,'scanword.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to 26 do s[i,0].p:=0;
for i:=1 to n do begin
readln(z);
j:=ord(z[1])-ord('a')+1;
s[j,0].p:=s[j,0].p+1;
s[j,s[j,0].p].k:=z;
readln(s[j,s[j,0].p].p);
end;
readln(m);
for i:=1 to m do begin
readln(z);
j:=ord(z[1])-ord('a')+1;
q:=1;
while s[j,q].k<>z do q:=q+1;
writeln(s[j,q].p);
end;
close(input);
close(output);
End.