#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, a, b, c, d, ans;
  string s;
  cin >> s;
  n = s.size();
  vector<int> v1, v2, v3;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') v1.push_back(i);
    if (s[i] == '2') v2.push_back(i);
    if (s[i] == '3') v3.push_back(i);
  }
  if (v1.empty() || v2.empty() || v3.empty())
    ans = 0;
  else {
    a = v1.size();
    b = v2.size();
    c = v3.size();
    ans = INT_MAX;
    for (int i = 0; i < a; i++) {
      int j = (lower_bound(v2.begin(), v2.end(), v1[i]) - v2.begin());
      int k = (lower_bound(v3.begin(), v3.end(), v1[i]) - v3.begin());
      if (j < b && k < c) {
        ans = min(ans, max(v2[j], v3[k]) - v1[i] + 1);
      }
    }
    for (int i = 0; i < b; i++) {
      int j = (lower_bound(v1.begin(), v1.end(), v2[i]) - v1.begin());
      int k = (lower_bound(v3.begin(), v3.end(), v2[i]) - v3.begin());
      if (j < a && k < c) {
        ans = min(ans, max(v1[j], v3[k]) - v2[i] + 1);
      }
    }
    for (int i = 0; i < c; i++) {
      int j = (lower_bound(v2.begin(), v2.end(), v3[i]) - v2.begin());
      int k = (lower_bound(v1.begin(), v1.end(), v3[i]) - v1.begin());
      if (j < b && k < a) {
        ans = min(ans, max(v2[j], v1[k]) - v3[i] + 1);
      }
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
