#include <bits/stdc++.h>
using namespace std;
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
  static_assert(D >= 1, "Vector dimension must be greater than zero!");
  template <typename... Args>
  Vec(int n = 0, Args... args)
      : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
  Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {}
};
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  if (y == 0) return 1;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int modinv(long long int a, long long int p) {
  return power(a, p - 2, p);
}
void solve() {
  string s;
  cin >> s;
  long long int n = s.size();
  Vec<2, long long int> right(n, 3, -1), left(n, 3, -1);
  left[0][s[0] - '1'] = 0;
  for (long long int i = 1; i < n; i++) {
    left[i][0] = left[i - 1][0];
    left[i][1] = left[i - 1][1];
    left[i][2] = left[i - 1][2];
    left[i][s[i] - '1'] = i;
  }
  right[n - 1][s[n - 1] - '1'] = n - 1;
  for (long long int i = n - 2; i >= 0; i--) {
    right[i][0] = right[i + 1][0];
    right[i][1] = right[i + 1][1];
    right[i][2] = right[i + 1][2];
    right[i][s[i] - '1'] = i;
  }
  long long int mn = 1000000000000000000;
  for (long long int i = 1; i < n - 1; i++) {
    if (s[i] == '1') {
      if (right[i][1] != -1 && left[i][2] != -1) {
        mn = min(mn, right[i][1] - left[i][2] + 1);
      }
      if (right[i][2] != -1 && left[i][1] != -1) {
        mn = min(mn, right[i][2] - left[i][1] + 1);
      }
    } else if (s[i] == '2') {
      if (right[i][0] != -1 && left[i][2] != -1) {
        mn = min(mn, right[i][0] - left[i][2] + 1);
      }
      if (right[i][2] != -1 && left[i][0] != -1) {
        mn = min(mn, right[i][2] - left[i][0] + 1);
      }
    } else if (s[i] == '3') {
      if (right[i][0] != -1 && left[i][1] != -1) {
        mn = min(mn, right[i][0] - left[i][1] + 1);
      }
      if (right[i][1] != -1 && left[i][0] != -1) {
        mn = min(mn, right[i][1] - left[i][0] + 1);
      }
    }
  }
  if (mn != 1000000000000000000) {
    cout << mn << endl;
  } else
    cout << 0 << endl;
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int T = 1;
  cin >> T;
  for (long long int c = 0; c < T; c++) {
    solve();
  }
  return 0;
}
