#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  return (!b) ? a : gcd(b, a % b);
}
int t = 1;
int ti;
const int N = 2e5 + 5;
int d[N][3];
void solve() {
  string s;
  cin >> s;
  set<char> st;
  for (int i = 0; i < (int)s.size(); i++) st.insert(s[i]);
  if ((int)st.size() < 3) {
    cout << 0 << '\n';
    return;
  }
  for (int i = 0; i <= 2; i++) d[(int)s.size()][i] = 0;
  for (int i = (int)s.size() - 1; i >= 0; i--) {
    for (int k = 0; k <= 2; k++) d[i][k] = d[i + 1][k];
    d[i][s[i] - '1'] = i;
  }
  int ans = (int)s.size();
  for (int i = 0; i <= (int)s.size() - 1; i++) {
    int minl = (int)s.size() - 1, maxl = 0;
    for (int k = 0; k <= 2; k++) {
      minl = min(minl, d[i][k]);
      maxl = max(maxl, d[i][k]);
    }
    if (maxl - minl + 1 >= 3) ans = min(ans, maxl - minl + 1);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cin >> t;
  for (ti = 1; ti <= t; ti++) solve();
}
