#H,W,N,D=map(int, input().split()) Q=int(input()) A=[] for i in range(10): A.append(str(i)) A.append('a') A.append('b') A.append('c') A.append('d') A.append('e') A.append('f') for i in range(Q): S=input() if S[:2]=='0b': S=S[2:][::-1] ans=0 for i in range(len(S)): ans+=pow(2,i)*A.index(S[i]) elif S[:2]=='0o': S=S[2:][::-1] ans=0 for i in range(len(S)): ans+=pow(8,i)*A.index(S[i]) elif S[:2]=='0x': S=S[2:][::-1] ans=0 for i in range(len(S)): ans+=pow(16,i)*A.index(S[i]) else: ans=int(S) print(ans)