#include <bits/stdc++.h>
using namespace std;
int t, n, m, k, a[300010], q, l;
string s;
map<int, int> h1, h2, h3;
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> t;
  for (; t; t--) {
    cin >> s;
    int c2 = 0, c1 = 0, c3 = 0;
    int ln = 300000;
    h1[0] = 0;
    h2[0] = 0;
    h3[0] = 0;
    for (int i = 0; i < s.length(); i++) {
      if ((s[i] - '0') == 3) {
        c3++;
        h3[c3] = i;
      }
      if ((s[i] - '0') == 2) {
        c2++;
        h2[c2] = i;
      }
      if ((s[i] - '0') == 1) {
        c1++;
        h1[c1] = i;
      }
      if (c2 == 0 || c1 == 0 || c3 == 0) {
        continue;
      }
      ln = min(ln, max(max(i - h2[c2] + 1, i - h3[c3] + 1), i - h1[c1] + 1));
    }
    if (ln > 200000) {
      cout << "0\n";
    } else {
      cout << ln << "\n";
    }
  }
  return 0;
}
