program scanword;
const
maxn = 10000;
type
rtype = record
s: string;
p: longint;
end;
var
n,m: longint;
w: array[1..maxn] of rtype;
procedure qsort(l,r:longint);
var
i,j: longint;
x: string;
temp:rtype;
begin
i :=l; j :=r; x :=w[(i+j) shr 1].s;
repeat
while w[i].s <x do inc(i);
while w[j].s >x do dec(j);
if i <=j then begin
temp :=w[i]; w[i] :=w[j]; w[j] :=temp;
inc(i); dec(j);
end;
until i >j;
if j >l then qsort(l,j);
if i <r then qsort(i,r);
end;
procedure init;
var
i: longint;
begin
readln(n);
for i :=1 to n do begin
readln(w[i].s);
readln(w[i].p);
end;
qsort(1,n);
end;
function find(s:string):longint;
var
l,r,mid: longint;
begin
l :=1; r :=n;
while true do begin
mid :=(l+r) shr 1;
if w[mid].s = s then exit(w[mid].p);
if w[mid].s <s then l :=mid+1
else r :=mid-1;
end;
end;
procedure main;
var
i: longint;
s: string;
begin
readln(m);
for i :=1 to m do begin
readln(s);
writeln(find(s));
end;
end;
begin
assign(input,'scanword.in'); reset(input);
assign(output,'scanword.out'); rewrite(output);
init;
main;
close(input); close(output);
end.