Q = int(input()) S = [0] * Q for i in range(Q): S[i] = input() ans = [] l0 = ["0b0", "0o0", "0", "0x0"] for i in range(Q): s = S[i] if s in l0: ans.append(0) continue if len(s) == 1: ans.append(s) continue if s[1] == "b": a = 2 elif s[1] == "o": a = 8 elif s[1] == "x": a = 16 else: ans.append(s) continue ans.append(int(s, a)) for i in range(Q): print(ans[i])