#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
uint32_t T = 1;
int t = 0;
void solve() {
  char s[200000 + 1];
  scanf("%s", &s);
  int min_dist = 3000000;
  int a = -1, b = -1, c = -1;
  for (int i = 0; s[i] != 0; ++i) {
    if (s[i] == '1') {
      a = i;
    } else if (s[i] == '2') {
      b = i;
    } else if (s[i] == '3') {
      c = i;
    }
    if (a != -1 && b != -1 && c != -1) {
      min_dist = min(min_dist, abs(max(a, max(b, c)) - min(min(a, b), c)) + 1);
    }
  }
  if (a == -1 || b == -1 || c == -1) {
    min_dist = 0;
  }
  printf("%d\n", min_dist);
}
int main() {
  scanf("%u\n", &T);
  for (; t < T; ++t) {
    solve();
  }
  return 0;
}
