Charlie and Johnny play a game. For every integer $\mbox{X}$ Charlie gives, Johnny has to find the smallest positive integer $\mathbf{Y}$ such that $X\times Y$ ($\mbox{X}$ multiplied by $\mathbf{Y}$) contains only 4s and 0s and starts with one or more 4s followed by zero or more 0s. For example, 404 is an invalid number but 4400, 440, and 444 are valid numbers. 

If $\boldsymbol{a}$ is the number of 4s and $\boldsymbol{b}$ is the number of 0s, can you print the value of $(2\times a)+b$?  

Input Format  

The first line of input contains a single integer $\mathbf{T}$, the number of test cases.  

$\mathbf{T}$ lines follow, each line containing the integer $\mbox{X}$ as stated above.

Output Format  

For every $\mbox{X}$, print the output $(2\times a)+b$ in a newline as stated in the problem statement.  

Constraints  

$1\leq T\leq100$ 

$1\leq X\leq10^{10}$  

Sample Input  

3
4
5
80

Sample Output  

2
3
4

Explanation  

For the 1^{st} test case, the smallest such multiple of $4}$ is 4 itself. Hence the value of $\boldsymbol{a}$ will be $\mbox{1}$ and and the value of $\boldsymbol{b}$ will be $\mbox{0}$, and the answer is $(2\times a)+b=2$.  

For the 2^{nd} test case, $Y=8$ and 40 is the minimum such multiple of $5$. Hence the values of $\boldsymbol{a}$, $\boldsymbol{b}$ and $(2\times a)+b$ will be $\mbox{1}$, $\mbox{1}$ and $3$ respectively.

Difficulty: MEDIUM_HARD

Solutions:
import fractions
maxN = 100000
isPrime = [True] * (maxN + 1)
isPrime[0] = isPrime[1] = False
prime = []
for i in range(2, maxN + 1):
	if isPrime[i]:
		for j in range(i + i, maxN + 1, i):
			isPrime[j] = False
		prime.append(i)

def check(x, l):
	(rem, p, e) = (0, 4, 10)
	while l:
		if l & 1 == 1:
			rem = (rem * e + p) % x
		p = (p * e + p) % x
		e = e * e % x
		l >>= 1
	return rem == 0

def solve(x):
	y = x
	(c2, c5) = (0, 0)
	while x % 2 == 0:
		c2 += 1
		x //= 2
	while x % 5 == 0:
		c5 += 1
		x //= 5
	zero = max(max(c2 - 2, 0), c5)
	(p, c) = ([], [])
	for i in range(len(prime)):
		if prime[i] * prime[i] > x or x <= 1:
			break
		if x % prime[i] == 0:
			cnt = 0
			p.append(prime[i])
			while x % prime[i] == 0:
				x //= prime[i]
				cnt += 1
			c.append(cnt)
	if x > 1:
		p.append(x)
		c.append(1)
	m = [0] * len(p)
	for i in range(0, len(p)):
		if p[i] == 3:
			m[i] = 3
			continue
		p1 = int(p[i] - 1)
		p2 = int(p1 ** 0.5)
		m[i] = p1
		flag = False
		for j in range(2, p2 + 1):
			if p1 % j == 0:
				flag = check(p[i], j)
				if flag:
					m[i] = j
					break
		if not flag:
			for j in range(p2 + 1, 1, -1):
				if p1 % j == 0:
					flag = check(p[i], p1 // j)
					if flag:
						m[i] = p1 // j
						break
	for i in range(len(p)):
		m[i] *= int(p[i]) ** int(c[i] - 1)
	four = 1
	if len(m) > 0:
		four = m[0]
	for i in range(1, len(p)):
		four = four * m[i] / fractions.gcd(four, m[i])
	return int(four) * 2 + zero
T = int(input())
for _ in range(T):
	x = int(input())
	print(solve(x))


import collections
import math
import operator as op
import sys
from functools import reduce

def get_prime_factors(n):
	factors = collections.defaultdict(int)
	k = 2
	while k * k <= n:
		while n % k == 0:
			factors[k] += 1
			n //= k
		k += 1
	if n > 1:
		factors[n] = 1
	return factors

def get_divisors(n):
	divisors = [1, n]
	for d in range(1, int(math.sqrt(n)) + 1):
		if n % d == 0:
			divisors.extend([d, n // d])
	return sorted(divisors)

def totient(n):
	return reduce(op.mul, [(k - 1) * k ** (v - 1) for (k, v) in get_prime_factors(n).items()], 1)
T = int(sys.stdin.readline())
for _ in range(T):
	X = int(sys.stdin.readline())
	if X == 1 or X == 2 or X == 4:
		print(2)
	else:
		(twos, fives) = (0, 0)
		while X % 2 == 0:
			X //= 2
			twos += 1
		while X % 5 == 0:
			X //= 5
			fives += 1
		b = max(twos - 2, fives)
		a = min((d for d in get_divisors(totient(9 * X)) if pow(10, d, 9 * X) == 1))
		print(2 * a + b)


import fractions
maxN = 100000
isPrime = [True] * (maxN + 1)
isPrime[0] = isPrime[1] = False
prime = []
for i in range(2, maxN + 1):
	if isPrime[i]:
		for j in range(i + i, maxN + 1, i):
			isPrime[j] = False
		prime.append(i)

def check(x, l):
	(rem, p, e) = (0, 4, 10)
	while l:
		if l & 1 == 1:
			rem = (rem * e + p) % x
		p = (p * e + p) % x
		e = e * e % x
		l >>= 1
	return rem == 0

def solve(x):
	y = x
	(c2, c5) = (0, 0)
	while x % 2 == 0:
		c2 += 1
		x //= 2
	while x % 5 == 0:
		c5 += 1
		x //= 5
	zero = max(max(c2 - 2, 0), c5)
	(p, c) = ([], [])
	for i in range(len(prime)):
		if prime[i] * prime[i] > x or x <= 1:
			break
		if x % prime[i] == 0:
			cnt = 0
			p.append(prime[i])
			while x % prime[i] == 0:
				x //= prime[i]
				cnt += 1
			c.append(cnt)
	if x > 1:
		p.append(x)
		c.append(1)
	m = [0] * len(p)
	for i in range(0, len(p)):
		if p[i] == 3:
			m[i] = 3
			continue
		p1 = int(p[i] - 1)
		p2 = int(p1 ** 0.5)
		m[i] = p1
		flag = False
		for j in range(2, p2 + 1):
			if p1 % j == 0:
				flag = check(p[i], j)
				if flag:
					m[i] = j
					break
		if not flag:
			for j in range(p2 + 1, 1, -1):
				if p1 % j == 0:
					flag = check(p[i], p1 // j)
					if flag:
						m[i] = p1 // j
						break
	for i in range(len(p)):
		m[i] *= int(p[i]) ** int(c[i] - 1)
	four = 1
	if len(m) > 0:
		four = m[0]
	for i in range(1, len(p)):
		four = four * m[i] / fractions.gcd(four, m[i])
	return int(four) * 2 + zero
T = int(input())
for _ in range(T):
	x = int(input())
	print(solve(x))


from math import gcd

def phi(n):
	nn = n
	if n % 2 == 0:
		nn //= 2
		while n % 2 == 0:
			n //= 2
	p = 3
	while p * p <= n:
		if n % p == 0:
			nn = nn // p * (p - 1)
			while n % p == 0:
				n //= p
		p += 2
	if n > 1:
		nn = nn // n * (n - 1)
	return nn

def divisors(n):
	v = []
	d = 1
	while d * d <= n:
		if n % d == 0:
			v.append(d)
			ndivd = n // d
			if ndivd > d:
				v.append(ndivd)
		d += 1
	v.sort()
	return v

def calc(x):
	g = gcd(x, 4)
	x //= g
	e2 = 0
	while x % 2 == 0:
		x //= 2
		e2 += 1
	e5 = 0
	while x % 5 == 0:
		x //= 5
		e5 += 1
	b = max(e2, e5)
	x *= 9
	phix = phi(x)
	vd = divisors(phix)
	for d in vd:
		if pow(10, d, x) == 1:
			a = d
			break
	return 2 * a + b
T = int(input())
for _ in range(T):
	X = int(input())
	print(calc(X))


import collections
import math
import operator as op
import sys
from functools import reduce

def get_prime_factors(n):
	factors = collections.defaultdict(int)
	k = 2
	while k * k <= n:
		while n % k == 0:
			factors[k] += 1
			n //= k
		k += 1
	if n > 1:
		factors[n] = 1
	return factors

def get_divisors(n):
	divisors = [1, n]
	for d in range(1, int(math.sqrt(n)) + 1):
		if n % d == 0:
			divisors.extend([d, n // d])
	return sorted(divisors)

def totient(n):
	return reduce(op.mul, [(k - 1) * k ** (v - 1) for (k, v) in get_prime_factors(n).items()], 1)
T = int(sys.stdin.readline())
for _ in range(T):
	X = int(sys.stdin.readline())
	if X == 1 or X == 2 or X == 4:
		print(2)
	else:
		(twos, fives) = (0, 0)
		while X % 2 == 0:
			X //= 2
			twos += 1
		while X % 5 == 0:
			X //= 5
			fives += 1
		b = max(twos - 2, fives)
		a = min((d for d in get_divisors(totient(9 * X)) if pow(10, d, 9 * X) == 1))
		print(2 * a + b)


def get_phi(n):
	ret = 1
	i = 2
	while i * i <= n:
		if n % i == 0:
			ret *= i - 1
			n //= i
			while n % i == 0:
				ret *= i
				n //= i
		i += 1
	if n > 1:
		ret *= n - 1
	return ret

def check_all_divisors(n, x):
	ret = n
	i = 1
	while i * i <= n:
		if n % i == 0:
			if pow(10, i, x) == 1:
				return i
			if pow(10, n // i, x) == 1:
				ret = n // i
		i += 1
	return ret
T = int(input())
for cn in range(1, T + 1):
	x = int(input())
	(two, five) = (0, 0)
	while x % 2 == 0:
		two += 1
		x //= 2
	while x % 5 == 0:
		x //= 5
		five += 1
	phi_x = get_phi(9 * x)
	a = check_all_divisors(get_phi(9 * x), 9 * x)
	b = max(two - 2, five)
	print(a + a + b)