#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
bool eval(string s, int len) {
  int cnt[4] = {0, 0, 0, 0};
  int n = s.size();
  for (int i = 0; i < len; i++) {
    cnt[s[i] - '0']++;
  }
  if (cnt[1] > 0 && cnt[2] > 0 && cnt[3] > 0) {
    return true;
  }
  for (int i = len; i < n; i++) {
    cnt[s[i - len] - '0']--;
    cnt[s[i] - '0']++;
    if (cnt[1] > 0 && cnt[2] > 0 && cnt[3] > 0) {
      return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int testCases;
  cin >> testCases;
  while (testCases--) {
    string s;
    cin >> s;
    int answer = 0;
    int low = 3, high = s.size();
    while (low <= high) {
      int mid = (low + high) >> 1;
      if (eval(s, mid)) {
        answer = mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    cout << answer << '\n';
  }
  return EXIT_SUCCESS;
}
