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

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