#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  unsigned long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int start = 0;
    int minlen = INT_MAX;
    int curr_count[4] = {0, 0, 0, 0};
    int flag = 0;
    int count = 0;
    for (int j = 0; j < s.length(); j++) {
      curr_count[int(s[j]) - 49]++;
      if (curr_count[int(s[j]) - 49] == 1) count++;
      if (count == 3) {
        flag = 1;
        while (curr_count[int(s[start]) - 49] > 1) {
          if (curr_count[int(s[start]) - 49] > 1)
            curr_count[int(s[start]) - 49]--;
          start++;
        }
        int len_window = j - start + 1;
        if (minlen > len_window) {
          minlen = len_window;
        }
      }
    }
    if (flag)
      cout << minlen << "\n";
    else
      cout << 0 << "\n";
  }
}
