from math import inf from string import ascii_uppercase alphabet_num = {letter: idx for idx, letter in enumerate(ascii_uppercase, 10)} alphabet_num.update({str(idx): idx for idx in range(10)}) num_alphabet = {num: letter for letter, num in alphabet_num.items()} def inference_base_10(num_str: str) -> int: minimum = inf for base_ in range(2, 37): try: minimum = min(minimum, calc_base_n(num_str, base_)) except ValueError: pass return minimum def calc_base_n(num_str: str, n: int) -> int: num_base_10 = 0 for idx, num in enumerate(reversed(num_str)): if alphabet_num[num] >= n: raise ValueError num_base_10 += alphabet_num[num] * n**idx return num_base_10 def main(): minimum = inf for _ in range(int(input())): num = input() minimum = min(minimum, inference_base_10(num)) print(minimum) if __name__ == "__main__": main()