tp = {}

while 1:

	t,p = list(map(int,input().split(",")))

	if t == p == 0: break

	tp[t] = p

plist = sorted(list(set([tp[key] for key in list(tp.keys())])),reverse = True)

while 1:

	try:

		team = int(input())

	except:

		break

	print(plist.index(tp[team])+1)
tp = {}

while 1:

	t,p = list(map(int,input().split(",")))

	if t==p==0:break

	tp[t]=p

ls=sorted(list(set([tp[key] for key in list(tp.keys())])),reverse=True)

while 1:

	try:team=int(input())

	except:break

	print(ls.index(tp[team])+1)
tp = {}
while 1:
    t, p = list(map(int, input().split(",")))
    if t == p == 0:
        break
    tp[t] = p
plist = sorted(list(set([tp[key] for key in list(tp.keys())])), reverse=True)
while 1:
    try:
        team = int(input())
    except:
        break
    print(plist.index(tp[team]) + 1)

tp = {}
while 1:
    t, p = list(map(int, input().split(",")))
    if t == p == 0:
        break
    tp[t] = p
ls = sorted(list(set([tp[key] for key in list(tp.keys())])), reverse=True)
while 1:
    try:
        team = int(input())
    except:
        break
    print(ls.index(tp[team]) + 1)

-plist = sorted(list(set([tp[key] for key in list(tp.keys())])), reverse=True) +ls = sorted(list(set([tp[key] for key in list(tp.keys())])), reverse=True) -    print(plist.index(tp[team]) + 1) +    print(ls.index(tp[team]) + 1)