#include <bits/stdc++.h>
using namespace std;
const long long N = 200100;
const long long MOD = 998244353;
vector<long long> v[10];
void solve() {
  string s;
  v[1].clear();
  v[2].clear();
  v[3].clear();
  cin >> s;
  for (long long i = 0; i < s.length(); i++) v[s[i] - '0'].push_back(i);
  if (v[1].empty() || v[2].empty() || v[3].empty()) {
    cout << "0\n";
    return;
  }
  for (long long i = 1; i <= 3; i++) sort(v[i].begin(), v[i].end());
  long long ans = s.length();
  long long i = v[1].size() - 1, j = v[2].size() - 1, k = v[3].size() - 1;
  while (i >= 0 && j >= 0 && k >= 0) {
    long long mn = min(v[1][i], min(v[2][j], v[3][k]));
    long long mx = max(v[1][i], max(v[2][j], v[3][k]));
    ans = min(ans, (mx - mn + 1));
    if (v[1][i] == mx) i--;
    if (v[2][j] == mx) j--;
    if (v[3][k] == mx) k--;
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
