#include <bits/stdc++.h>
using namespace std;
vector<long long> g[100007];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long i, j;
    i = 0;
    j = 0;
    long long n = s.length();
    long long o = 0, tw = 0, thr = 0;
    long long mn = LLONG_MAX;
    while (j < n) {
      if (o > 0 && tw > 0 && thr > 0) {
        o -= (s[i] == '1');
        tw -= (s[i] == '2');
        thr -= (s[i] == '3');
        mn = min(j - i, mn);
        i++;
      } else {
        o += (s[j] == '1');
        tw += (s[j] == '2');
        thr += (s[j] == '3');
        j++;
      }
    }
    while (o > 0 && tw > 0 && thr > 0) {
      o -= (s[i] == '1');
      tw -= (s[i] == '2');
      thr -= (s[i] == '3');
      mn = min(j - i, mn);
      i++;
    }
    if (mn == LLONG_MAX) {
      mn = 0;
    }
    cout << mn << '\n';
  }
}
