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
x3 = 0
for i in range(1, 100001):
    mod1 = i % 10
    mod2 = i % 100
    if ((mod2 >= 44) and (mod2 < 77)):
        result += x3
        x3 += 1   
    elif ((mod1 >= 4) and (mod1 < 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])
        