#include "bits/stdc++.h"
using namespace std;
int y, res;
unordered_map<char, int> h;
int main() {
cin >> y;
while (y --) {
h.clear();
string s;
cin >> s;
int n = s.size();
res = n + 1;
for (int i = 0, j = 0; i < n; i ++) {
h[s[i]] ++;
while (h['1'] && h['2'] && h['3']) {
res = min(res, i + 1 - j);
h[s[j]]--;
j++;
}
}
if (res == n + 1) cout << 0 << endl;
else cout << res << endl;
}
return 0;
}