#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    s += 'o';
    long long int l = 0, r = 0, ans = INT_MAX, n = s.length(), i;
    bool ok;
    map<char, long long int> m;
    while (l <= r && r < n) {
      ok = true;
      for (char i = '1'; i <= '3'; i++) {
        if (m[i] < 1) ok = false;
      }
      if (!ok) {
        m[s[r]]++;
        ok = true;
        for (char i = '1'; i <= '3'; i++) {
          if (m[i] < 1) ok = false;
        }
        if (ok) ans = min(ans, r - l + 1);
        r += 1;
      } else {
        ans = min(ans, r - l);
        m[s[l]]--;
        l++;
      }
    }
    if (ans == INT_MAX)
      cout << "0" << endl;
    else
      cout << ans << endl;
  }
}
