program scanword(input,output);
var
a,c:array[1..10000] of string;
p:array[1..10000] of longint;
b:array[1..26,1..2000] of string;
e:array[1..26,1..2000] of longint;
d:array[1..26] of longint;
i,j,l,m,n:longint;
procedure tp(t:longint);
var k:longint;
begin
for j:=1 to m do begin
k:=ord(a[j][1])-96;
inc(d[k]);
b[k,d[k]]:=a[j];
e[k,d[k]]:=p[j];
end;
end;
begin
assign(input,'scanword.in');assign(output,'scanword.out');
reset(input);rewrite(output);
readln(m);
for i:=1 to m do begin readln(a[i]); readln(p[i]); end;
readln(n);
for i:=1 to n do readln(c[i]);
tp(m);
for i:=1 to n do begin
l:=ord(c[i][1])-96;
for j:=1 to d[l] do
if b[l,j]=c[i] then writeln(e[l,j]);
end;
close(input);close(output);
end.