#阿里巴巴# 8月10日第二题s=input().split(" ")node_num=int(s[0])round=int(s[1])list1=[int(i) for i in (input().split(" "))]d={}for i in range(node_num-1): d[i+2]=list1[i]print(d)def play(a,b): global d flag=True if a==1: return 'A' if b==1: return 'B' while flag: if d[a]==1 and d[b]==1: return 'A' flag = False elif d[a]==1: return 'A' flag = False elif d[b]==1: return 'B' flag = False else : a,b=d[a],d[b]while round: s=input().split(" ") a=int(s[0]) b=int(s[1]) print(play(a,b)) round-=1