#include <bits/stdc++.h>
using namespace std;
int pref[200003][4];
bool check(int i, int j) {
  int res = pref[j][1], res2 = pref[j][2], res3 = pref[j][3];
  if (i) {
    res = pref[j][1] - pref[i - 1][1];
    res2 = pref[j][2] - pref[i - 1][2];
    res3 = pref[j][3] - pref[i - 1][3];
  }
  return (res && res2 && res3);
}
int main() {
  ios::sync_with_stdio(0), cin.tie();
  int t;
  cin >> t;
  while (t--) {
    memset(pref, 0, sizeof pref);
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
      pref[i][s[i] - '0']++;
      if (i) {
        pref[i][1] += pref[i - 1][1];
        pref[i][2] += pref[i - 1][2];
        pref[i][3] += pref[i - 1][3];
      }
    }
    int res = 1e9;
    int n = s.size();
    for (int i = 0; i < n; i++) {
      int l = i, h = n - 1;
      while (l <= h) {
        int mid = (l + h) / 2;
        if (check(i, mid)) {
          res = min(res, mid - i + 1);
          h = mid - 1;
        } else
          l = mid + 1;
      }
    }
    if (res == 1e9)
      cout << "0\n";
    else
      cout << res << endl;
  }
  return 0;
}
