#include <bits/stdc++.h>
using namespace std;
const unsigned int M = 1000000007;
bool isPrime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
const int N = 1e6 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    set<char> second;
    int maxi = INT_MAX;
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
      second.insert(s[i]);
    }
    if (second.size() == 1) {
      cout << 0 << "\n";
      continue;
    }
    int a = -1, b = -1, c = -1;
    int flag = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        a = i;
      } else if (s[i] == '2') {
        b = i;
      } else {
        c = i;
      }
      if (a != -1 && b != -1 && c != -1) {
        int l = max(a, max(b, c));
        int r = min(a, min(b, c));
        maxi = min(maxi, l - r + 1);
        flag = 1;
      }
    }
    if (flag) {
      cout << maxi << "\n";
    } else {
      cout << 0 << "\n";
    }
  }
}
