#include <bits/stdc++.h>
using namespace std;
int p, cnt[4];
string s;
void input() { cin >> s; }
void addChar(char c) {
  if (++cnt[c - '0'] == 1) ++p;
}
void remChar(char c) {
  if (--cnt[c - '0'] == 0) --p;
}
int xl() {
  int res = 1e9, n = s.size();
  int j = -1;
  cnt[1] = 0, cnt[2] = 0, cnt[3] = 0;
  p = 0;
  for (int i = 0; i < n; ++i) {
    while (j < n - 1 && p < 3) {
      ++j;
      addChar(s[j]);
    }
    if (p == 3) res = min(res, j - i + 1);
    remChar(s[i]);
  }
  return (res != 1e9) ? res : 0;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    input();
    cout << xl();
    cout << "\n";
  }
  return 0;
}
