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

def equals(x, y):
    for i in range(index, -1, -1):
        mod1 = x % list10[i]
        mod2 = y % list10[i]
        if ((mod1 > list4[i]) and (mod1 <= list7[i])):
            group1 = 2
        else:
            group1 = 1 
        if ((mod2 >= list4[i]) and (mod2 < list7[i])):
            group2 = 1
        else:
            group2 = 2
        if (group1 == group2):
            return False
    return True
    
for j in range(1, 100001):
    for k in range(j, n+1):
        if (k == list4[index + 1]):
            index += 1 
        if equals(j, k):
            result += 1
    results.append(result) 

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