#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MOD = 1e9 + 7;

void solve(){
	int n;
	cin >> n;
	int m = to_string(n).size();
	int ans = INT_MAX;
	int len = 1;
	
	string h = to_string(n);
	
	for(auto x: h){
		if(x == '7'){
			cout << 0 << "\n";
			return;
		}
	}
	for(int i = 0; i <= 2 * m; i++){
		ll tmp = n;
		for(int j = 0; j < 500; j++){
			ll g = pow(10, len);
			g--;
			tmp += g;
			string h = to_string(tmp);
			if(h.size() >= 17)break;
			bool done = false;
			for(auto x: h){
				if(x == '7'){
					done = true;
					ans = min(ans, j + 1);
					break;
				}
			}
			if(done){
				break;
			}
			
		}
		len++;
	
	}
	cout << ans << "\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t = 1;
	cin >> t;
	
	for(int i = 1; i <= t; i++){
		solve();
	}
	return 0;
}