#include <bits/stdc++.h>
using namespace std;
string str;
int main() {
  long long int i, j, t, n;
  cin >> t;
  while (t--) {
    cin >> str;
    int ar[4];
    for (i = 0; i < 4; i++) {
      ar[i] = 0;
    }
    int l = str.length();
    int count = 0;
    int mn = l + 1;
    for (i = 0, j = 0; i <= j && j < l; j++) {
      ar[str[j] - '1']++;
      if (ar[str[j] - '1'] == 1) {
        count++;
      }
      if (count == 3) {
        if (j - i + 1 < mn) {
          mn = j - i + 1;
        }
        while (i <= j && count == 3) {
          ar[str[i] - '1']--;
          if (ar[str[i] - '1'] == 0) {
            count--;
          }
          i++;
          if (count == 3 && j - i + 1 < mn) {
            mn = j - i + 1;
          }
        }
      }
    }
    if (mn > l) {
      cout << 0 << "\n";
    } else {
      cout << mn << "\n";
    }
  }
  return 0;
}
