#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, i, j;
  char s[200000];
  cin >> t;
  while (t--) {
    long long len = INT_MAX;
    cin >> s;
    vector<int> vec(4, 0), freq(4, 0);
    map<int, vector<int>> mp;
    for (i = 0; s[i] != '\0'; i++) {
      mp[s[i] - '0'].push_back(i);
      long long a = (s[i] - '0');
      vec[a]++;
    }
    if (vec[3] == 0 || vec[1] == 0 || vec[2] == 0) {
      cout << 0 << endl;
      continue;
    }
    i = 0, j = 0;
    freq[s[0] - '0']++;
    while (s[j] != '\0') {
      if (freq[1] == 0 || freq[2] == 0 || freq[3] == 0) {
        j++;
        freq[s[j] - '0']++;
      }
      if (freq[1] >= 1 && freq[2] >= 1 && freq[3] >= 1) {
        len = min(len, j - i);
        freq[s[i] - '0']--;
        i++;
      }
    }
    cout << len + 1 << endl;
  }
  return 0;
}
