#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  cin >> s;
  int l = 0, r = 0, ans = 1e6;
  vector<int> v(4, 0);
  v[s[0] - '0']++;
  while (r != s.size() && l != s.size()) {
    if (v[1] && v[2] && v[3]) {
      v[s[l] - '0']--;
      if (ans > r - l + 1) ans = r - l + 1;
      l++;
    } else {
      r++;
      if (r != s.size()) v[s[r] - '0']++;
    }
  }
  if (ans == (int)1e6) ans = 0;
  cout << ans << '\n';
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
