#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 3;
const long long inf = 1e18;
int t;
char s[maxn];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%s", &s);
    int n = strlen(s);
    int ans = INT_MAX;
    vector<int> L = {-1, -1, -1};
    for (int i = 0; i < n; i++) {
      L[s[i] - '1'] = i;
      int stuff = INT_MAX;
      for (char c = '1'; c <= '3'; c++) {
        if (c == s[i]) continue;
        stuff = min(stuff, L[c - '1']);
      }
      if (stuff == -1) continue;
      ans = min(ans, i - stuff + 1);
    }
    if (ans == INT_MAX) ans = 0;
    printf("%d\n", ans);
  }
  return 0;
}
