program scanword(input,output);
const h=13999;
var
word:string;
page,i,n,m:longint;
w:array[0..100000] of string;
p:array[0..100000] of longint;
function hash(word:string):longint;
var i:longint;
begin
hash:=ord(word[1])-96;
for i:=2 to 3 do
hash:=(hash*27+ord(word[i*2-1])-96) mod h;
end;
procedure makeh(word:string; page:longint);
var i,hc:longint;
begin
hc:=hash(word);
while w[hc]<>'' do hc:=(hc+1) mod h;
w[hc]:=word;
p[hc]:=page;
end;
function find(word:string):longint;
var hc:longint;
begin
hc:=hash(word);
while (w[hc]<>'')and(w[hc]<>word) do hc:=(hc+1) mod h;
find:=p[hc];
end;
begin
assign(input,'scanword.in');assign(output,'scanword.out');
reset(input);rewrite(output);
readln(n);
for i:=1 to n do begin
readln(word);
readln(page);
makeh(word,page);
end;
readln(m);
for i:=1 to m do begin
readln(word);
writeln(find(word));
end;
close(input);close(output);
end.