Separatist

Always finding new ways to destroy the Republic forces by surprise attacks.

Now they trapped the Republic fleet near the Abregado system. They have developed a way to communicate through the encrypted message to its fleet around the Abregado system.

They wrote a message in a cryptic language, and next to it they wrote a series of symbols. Republic fleet intercept these messages and concluded that the symbols indicate a number: the number of seconds before they launch next surprise attack on them!

Unfortunately Republic Fleet have no idea what each symbol means. They have decided that each symbol indicates one digit, but they aren't sure what each digit means or what base the Separatists are using. For example, if Separatist wrote "ab2ac999", they could have meant "31536000" in base 10 -- exactly one year -- or they could have meant "12314555" in base 6 -- 398951 seconds, or about four and a half days. Republic fleet are sure of three things: the number is positive; like us, the Separatists will never start a number with a zero; and they aren't using unary (base 1).

Your job is to determine the minimum possible number of seconds they launch next surprise attack on them.

Input

The first line of input contains a single integer, T. T test cases follow. Each test case is a string on a line by itself. The line will contain only characters in the 'a' to 'z' and '0' to '9' ranges (with no spaces and no punctuation), representing the message by Separatists. The test cases are independent, and can be in different bases with the symbols meaning different things.

Output

For each test case, output V the minimum number of seconds before the next surprise attack.

Limits

1 ≤ T ≤ 100
The answer will never exceed 10^18

1 ≤ the length of each line < 61

SAMPLE INPUT
3
11001001
cats
zig

SAMPLE OUTPUT
201
75
11
'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
n = input()
ans = []
def find(a):
	f = 0
	n = []
	l = []
	k = []
	t = 1
	for i in a:
		f = 0
		try:
			l.index(i)
		except ValueError:
			f = 1
			if t == 1:
				l.append(i)
				n.append(t)
				k.append(t)
				t = 0
			elif t == 0:
				l.append(i)
				n.append(t)
				k.append(t)
				t = 2
			elif t > 1:
				l.append(i)
				n.append(t)
				k.append(t)
				t += 1
		if f == 0:
			d = l.index(i)
			f = 0
			n.append(k[d])
	n1 = max(k)
	n1 += 1
	#print n1, "@@@@"
	n.reverse()
	sum = 0
	for i in range(len(n)):
		sum += n[i] * (n1 ** i)
		#print sum, "%%%%%"
	#print n, l, sum, k, "####"
	return sum
for i in range(n):
	a = raw_input()
	ans.append(find(a))
for i in ans:
	print i