#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int _ = 1;
  cin >> _;
  while (_--) {
    string s;
    cin >> s;
    vector<int> cnt(4);
    int need = 3;
    int n = s.size();
    int r = 0;
    int ans = 1e9;
    for (int l = 0; l < n; l++) {
      if (l > 0) {
        need += (cnt[s[l - 1] - '1']-- == 1);
      }
      while (r < n && need) {
        need -= (cnt[s[r] - '1']++ == 0);
        r++;
      }
      if (need) {
        break;
      }
      ans = min(ans, r - l);
    }
    if (ans == 1e9) {
      ans = 0;
    }
    cout << ans << endl;
  }
}
