#include <bits/stdc++.h>
using namespace std;
long const M = 1e9 + 7;
const long mxN = 1e5 + 2;
void solve() {
  string s;
  cin >> s;
  bool ok1 = 0, ok2 = 0, ok3 = 0;
  for (char x : s) {
    if (x == '1') ok1 = 1;
    if (x == '2') ok2 = 1;
    if (x == '3') ok3 = 1;
  }
  if (ok1 == 0 or ok2 == 0 or ok3 == 0) {
    cout << 0 << "\n";
    return;
  }
  vector<long long> v;
  long long n = (s).size();
  map<long long, long long> m;
  long long index = 0;
  for (long long i = 0; i < n;) {
    long long cnt = 1;
    for (; s[i] == s[i + 1]; i++) cnt++;
    m[index] = cnt;
    v.push_back(s[i] - '0');
    i++;
    index++;
  }
  long long ans = 1e9;
  for (long long i = 0; i < (v).size() - 2; i++) {
    if (v[i] + v[i + 1] + v[i + 2] == 6) {
      ans = min(ans, 1 + m[i + 1] + 1);
    }
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  for (long long i = 0; i < (long long)t; i++) {
    solve();
  }
  return 0;
}
