#include <bits/stdc++.h>
using namespace std;
long long int expo(long long int n) {
  long long int t;
  if (n == 0)
    return 1;
  else if (n == 1)
    return 2;
  t = expo(n / 2);
  if (n % 2) {
    return t * t * 2;
  } else
    return t * t;
}
long long int exponentMod(long long int A, long long int B, long long int C) {
  if (A == 0) return 0;
  if (B == 0) return 1;
  long long int y;
  if (B % 2 == 0) {
    y = exponentMod(A, B / 2, C);
    y = (y * y) % C;
  } else {
    y = A % C;
    y = (y * exponentMod(A, B - 1, C) % C) % C;
  }
  return (long long int)((y + C) % C);
}
int main() {
  long long int t;
  t = 1;
  long long int a, b, c, d;
  cin >> t;
  while (t--) {
    a = b = c = -1;
    string s;
    cin >> s;
    long long int ans = INT_MAX, fl = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '1') {
        a = i;
      } else if (s[i] == '2') {
        b = i;
      } else if (s[i] == '3') {
        c = i;
      }
      if (a != -1 && b != -1 && c != -1) {
        fl = 1;
        long long int l = min(a, min(b, c));
        long long int r = max(a, max(b, c));
        ans = min(ans, (r - l));
      }
    }
    if (fl == 0) {
      cout << 0 << "\n";
    } else
      cout << ans + 1 << "\n";
  }
}
