#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long inf64 = 1e18;
const long long MOD = inf + 7;
void solve() {
  string s;
  cin >> s;
  long long n = (long long)(s).size();
  long long ans = inf64;
  long long a[3] = {0};
  for (long long i = 0, j = 0; i < n; i++) {
    while (j < n && (!a[0] || !a[1] || !a[2])) {
      a[s[j] - '1']++;
      j++;
    }
    if (a[0] && a[1] && a[2]) ans = min(ans, j - i);
    a[s[i] - '1']--;
  }
  if (ans == inf64) ans = 0;
  cout << ans << "\n";
}
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  ;
  long long t = 1;
  cin >> t;
  while (t--) solve();
}
