#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000 + 7;
bool isvalid(int len, string &s) {
  int freq[4] = {0};
  int n = s.size();
  int i = 0;
  while (i < len) {
    freq[s[i] - '0']++;
    i++;
  }
  if (freq[1] > 0 && freq[2] > 0 && freq[3] > 0) return true;
  for (; i < n; i++) {
    freq[s[i - len] - '0']--;
    freq[s[i] - '0']++;
    if (freq[1] > 0 && freq[2] > 0 && freq[3] > 0) return true;
  }
  return false;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int st = 3;
    int end = s.size();
    int ans = inf;
    while (st <= end) {
      int mid = st + (end - st) / 2;
      if (isvalid(mid, s)) {
        end = mid - 1;
        ans = mid;
      } else
        st = mid + 1;
    }
    if (ans == inf)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
}
