#include <bits/stdc++.h>
using namespace std;
int t;
int f[4];
string s;
void Solve() {
  memset(f, 0, sizeof(f));
  int lgfinal = 1e9;
  int lg;
  int start = 0;
  for (int i = 0; i < s.size(); i++) {
    while (start < s.size() && (f[1] == 0 || f[2] == 0 || f[3] == 0)) {
      f[s[start] - '0']++;
      start++;
    }
    if (f[1] != 0 && f[2] != 0 && f[3] != 0) {
      lg = start - i;
      lgfinal = min(lgfinal, lg);
    }
    f[s[i] - '0']--;
  }
  if (lgfinal == 1e9)
    cout << 0 << '\n';
  else
    cout << lgfinal << '\n';
}
int main() {
  cin >> t;
  while (t--) {
    cin >> s;
    Solve();
  }
  return 0;
}
