#include<bits/stdc++.h>
using namespace std;
int judge(int n){
int temp;
while(n){
temp=n%10;
n=n/10;
if(temp==7){
return 0;
break;
}
}
return 1;
}
int main(){
int t;
long long a[10000];
cin>>t;
int n;
for(n=0;n<t;n++){
cin>>a[n];
}
for(int i=0;i<n;i++){
int count=0;
for(int j=1;j<=a[i];j++){
if(judge(j)){
count++;
}
}
cout<<count<<endl;
}
return 0;
}