#include <bits/stdc++.h>
using namespace std;
int t, a[4][200010], len;
string s;
bool check(int x) {
  for (int i = x; i <= len; i++)
    if (a[1][i] - a[1][i - x] && a[2][i] - a[2][i - x] && a[3][i] - a[3][i - x])
      return 1;
  return 0;
}
int main() {
  scanf("%d", &t);
  while (t--) {
    cin >> s;
    len = s.size();
    s += s[s.size() - 1];
    for (int i = len; i >= 1; i--) {
      s[i] = s[i - 1];
    }
    int l = 1, r = len;
    for (int i = 1; i <= len; i++) {
      if (s[i] == '1') {
        a[1][i] = a[1][i - 1] + 1;
        a[2][i] = a[2][i - 1];
        a[3][i] = a[3][i - 1];
      }
      if (s[i] == '2') {
        a[1][i] = a[1][i - 1];
        a[2][i] = a[2][i - 1] + 1;
        a[3][i] = a[3][i - 1];
      }
      if (s[i] == '3') {
        a[1][i] = a[1][i - 1];
        a[2][i] = a[2][i - 1];
        a[3][i] = a[3][i - 1] + 1;
      }
    }
    if (!a[1][len] || !a[2][len] || !a[3][len]) {
      puts("0");
      continue;
    }
    while (l + 1 < r) {
      int mid = (l + r) >> 1;
      if (check(mid))
        r = mid;
      else
        l = mid;
    }
    cout << r << endl;
    memset(a, 0, sizeof(a));
  }
  return 0;
}
