Q = int(input()) for _ in range(Q): S = input() if len(S) <= 2 or S[0] != "0": print(S) continue cmd = S[1] num = list(S[2:]) num.reverse() # print(num) now = 0 if cmd == "b": k = 2 elif cmd == "o": k = 8 elif cmd == "x": k = 16 else: print(S) continue for i, n in enumerate(num): if n == "a": n = "10" elif n == "b": n = "11" elif n == "c": n = "12" elif n == "d": n = "13" elif n == "e": n = "14" elif n == "f": n = "15" n = int(n) now += n * k**i print(now)