x,y=map(int,input().split())
xs=[]
d1={10:"A",9:"A",8:"B",7:"C",6:"OK",5:"ARE YOU PIG?"}
for i in range(x):
a,b,c=map(str,input().split())
c=int(c)
xs.append([a,b,c])
for j in range(y):
a=input()
for k in range(len(xs)):
if a==xs[k][0]:
# print(xs[k][2])
d=5 if xs[k][2]//10<=5 else xs[k][2]//10
print(d1[d])