#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t != 0) {
    string s;
    cin >> s;
    int c[4] = {0};
    for (int i = 0; i < s.length(); i++) {
      c[s[i] - '0']++;
    }
    bool ans = true;
    for (int i = 1; i <= 3; i++) {
      if (c[i] == 0) {
        ans = false;
        break;
      }
    }
    if (ans == false) {
      cout << 0;
    } else {
      int start = 0;
      int end = 0;
      int n = s.length();
      int ans = n;
      memset(c, 0, sizeof c);
      while (end < n) {
        c[s[end] - '0']++;
        if (c[1] >= 1 && c[2] >= 1 && c[3] >= 1) {
          while (start <= end && c[s[start] - '0'] > 1) {
            c[s[start] - '0']--;
            start++;
          }
          ans = min(ans, end - start + 1);
        }
        end++;
      }
      cout << ans;
    }
    cout << "\n";
    t--;
  }
  return 0;
}
