#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
char s[N];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    int res = len + 1;
    int a[3] = {-1, -1, -1};
    for (int i = 1; i <= len; i++) {
      int tmp = s[i] - '1';
      a[tmp] = i;
      int mm = len, mx = 0, flag = 1;
      for (int j = 0; j < 3; j++) {
        if (a[j] < 0) {
          flag = 0;
        }
        if (a[j] > 0) mm = min(mm, a[j]);
        if (a[j] > 0) mx = max(mx, a[j]);
      }
      if (flag) {
        res = min(res, mx - mm + 1);
      }
    }
    if (res == len + 1)
      puts("0");
    else
      printf("%d\n", res);
  }
  return 0;
}
