#include <bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	while (t--) {
		string s;
		cin >> s;
		int min_len = INT_MAX;
		int left = 0;
		int counts[4] = {0, 0, 0, 0};
		for (int right = 0; right < s.length(); ++right) {
			counts[s[right] - '0']++;
			while (counts[1] > 0 && counts[2] > 0 && counts[3] > 0) {
				min_len = min(min_len, right - left + 1);
				counts[s[left] - '0']--;
				left++;
			}
		}
		if (min_len == INT_MAX) {
			cout << 0 << endl;
		} else {
			cout << min_len << endl;
		}
	}
	return 0;
}