#include <bits/stdc++.h>
using namespace std;
void solv() {
  string s;
  cin >> s;
  long long n = s.size();
  long long l = 3, r = n, ans = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    vector<long long> cnt(3, 0);
    bool ok = 0;
    for (long long i = 0; i < n; i++) {
      cnt[s[i] - '1']++;
      if (i >= mid) cnt[s[i - mid] - '1']--;
      if (cnt[0] != 0 and cnt[1] != 0 and cnt[2] != 0) {
        ok = 1;
        break;
      }
    }
    if (ok) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << '\n';
}
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) solv();
}
