def s2m(s): for i,m in enumerate([chr(c) for c in range(90,64,-1)]+list("9876543210")): if m in s: return sum((int(c) if c.isdigit() else (ord(c)-55))*(36-i)**j for j,c in enumerate(s)) N = input() print min(s2m(raw_input()[::-1]) for i in range(N))