program scanword;
var
a:array['a'..'z',1..10000]of string;
n:array['a'..'z',1..10000]of longint;
m:array['a'..'z']of longint;
i,j,nn,mm,k,q:longint;
s:string;
aaa:char;
begin
assign(input,'scanword.in');
assign(output,'scanword.out');
reset(input); rewrite(output);
readln(nn);
for aaa:='a' to 'z' do begin
for j:=1 to nn do a[aaa,j]:='';
end;
fillchar(n,sizeof(n),0);
fillchar(m,sizeof(m),0);
for i:=1 to nn do begin
readln(s);
readln(k);
inc(m[s[1]]);
a[s[1],m[s[1]]]:=s;
n[s[1],m[s[1]]]:=k;
end;
readln(mm);
for i:=1 to mm do begin
readln(s);
for j:=1 to m[s[1]] do if a[s[1],j]=s then writeln(n[s[1],j]);
end;
close(input); close(output);
End.