#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
const long long INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const long long MOD2 = 998244353;
const long long N = 1000 * 100 + 10;
const long long N2 = 1000000 + 7;
const long double PI = 3.141592653589793;
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long n = s.length();
    map<char, long long> make_pair;
    long long count = 0;
    long long ans = INF;
    long long l[4];
    long long flag = 0;
    memset(l, -1, sizeof(l));
    for (long long i = 0; i < n; i++) {
      make_pair[s[i]]++;
      if (s[i] == '1') {
        l[1] = i;
      }
      if (s[i] == '2') {
        l[2] = i;
      }
      if (s[i] == '3') {
        l[3] = i;
      }
      if (make_pair['1'] >= 1 && make_pair['2'] >= 1 && make_pair['3'] >= 1) {
        flag++;
        ans = min(ans, i - min(l[1], min(l[2], l[3])));
      }
    }
    if (flag)
      cout << ans + 1 << "\n";
    else
      cout << 0 << "\n";
  }
  return 0;
}
