list4 = [4, 44, 444, 4444, 44444]
list7 = [7, 77, 777, 7777, 77777]
list10 = [10, 100, 1000, 10000, 100000]
result = 0
results = [] 

x1 = 1
x2 = 0
for i in range(1, 100001):
    mod = i % 10
    if ((mod >= 4) and (mod < 7)):
        result += x2
        x2 += 1
    else:
        result += x1
        x1 += 1
    results.append(result)

t = int(input(''))
for i in range(t):
    n = int(input(''))
    print(results[n-1])
        