#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int n = s.length();
    long long int ct1 = 0, ct2 = 0, ct3 = 0;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '1')
        ct1++;
      else if (s[i] == '2')
        ct2++;
      else
        ct3++;
    }
    if (ct1 == 0 || ct2 == 0 || ct3 == 0) {
      cout << "0"
           << "\n";
      continue;
    }
    vector<long long int> v;
    map<char, long long int> m;
    for (long long int i = 0; i < n; i++) {
      m[s[i]] = i;
      if (m.size() == 3) {
        long long int mini = INT_MAX, maxi = INT_MIN;
        for (auto it = m.begin(); it != m.end(); it++) {
          mini = min(mini, it->second);
          maxi = max(maxi, it->second);
        }
        v.push_back(maxi - mini + 1);
        if (maxi - mini + 1 == 3) {
          break;
        }
        auto it1 = m.find(s[mini]);
        m.erase(it1);
      }
    }
    long long int ans = INT_MAX;
    for (long long int i : v) {
      ans = min(ans, i);
    }
    cout << ans << "\n";
  }
  return 0;
}
