I=input for i in range(int(I())): S=I()+"00" s=S[1] B=[[16,8][s<"x"],2][s<"o"] print(int(S[:-2])if s<"b" else int(S[2:],B)//B//B)