var
m,n,i,j:longint;
s:array[1..10000] of ansistring;
c:array[1..10000] of longint;
head,tail:array['a'..'z'] of longint;
flag:char;
ca:ansistring;
procedure swap(var a,b:longint);
var
temp:longint;
begin
temp:=a;
a:=b;
b:=temp;
end;
procedure qsort(l,r:longint);
var
i,j:longint;
x,y:ansistring;
begin
i:=l;
j:=r;
x:=s[(l+r) div 2];
repeat
while s[i]<x do
inc(i);
while x<s[j] do
dec(j);
if i<=j then
begin
y:=s[i];
s[i]:=s[j];
s[j]:=y;
swap(c[i],c[j]);
inc(i);
dec(j);
end;
until i>j;
if l<j then
qsort(l,j);
if i<r then
qsort(i,r);
end;
function find(l,r:longint):longint;
var
x:longint;
begin
if l=r then
exit(l);
x:=(l+r) div 2;
if s[x]=ca then
exit(x);
if s[x]<ca then
exit(find(x+1,r))
else
exit(find(l,x-1));
end;
begin
assign(input,'scanword.in');
assign(output,'scanword.out');
reset(input);
rewrite(output);
//while not(eof) do
//begin
readln(n);
for i:=1 to n do
begin
readln(s[i]);
readln(c[i]);
end;
qsort(1,n);
flag:=s[1][1];
head[flag]:=1;
tail[s[n][1]]:=n;
for i:=2 to n do
if flag<>s[i][1] then
begin
tail[flag]:=i-1;
flag:=s[i][1];
head[flag]:=i;
if flag='z' then
break;
end;
readln(m);
for i:=1 to m do
begin
readln(ca);
writeln(c[find(head[ca[1]],tail[ca[1]])]);
end;
//end;
close(input);
close(output);
end.