#include <bits/stdc++.h>
using namespace std;
const int no_of_chars = 256;
string findSubString(string str, string pat) {
  int len1 = str.length();
  int len2 = pat.length();
  if (len1 < len2) {
    return "";
  }
  int hash_pat[no_of_chars] = {0};
  int hash_str[no_of_chars] = {0};
  for (int i = 0; i < len2; i++) hash_pat[pat[i]]++;
  int start = 0, start_index = -1, min_len = INT_MAX;
  int count = 0;
  for (int j = 0; j < len1; j++) {
    hash_str[str[j]]++;
    if (hash_pat[str[j]] != 0 && hash_str[str[j]] <= hash_pat[str[j]]) count++;
    if (count == len2) {
      while (hash_str[str[start]] > hash_pat[str[start]] ||
             hash_pat[str[start]] == 0) {
        if (hash_str[str[start]] > hash_pat[str[start]]) hash_str[str[start]]--;
        start++;
      }
      int len_window = j - start + 1;
      if (min_len > len_window) {
        min_len = len_window;
        start_index = start;
      }
    }
  }
  if (start_index == -1) {
    return "";
  }
  return str.substr(start_index, min_len);
}
void solve() {
  int l;
  string s;
  cin >> s;
  l = s.length();
  int u = 0, d = 0, t = 0;
  char c;
  for (int i = 0; i < l; i++) {
    c = s[i];
    if (c - '0' == 1)
      u++;
    else if (c - '0' == 2)
      d++;
    else
      t++;
  }
  if (!u || !d || !t) {
    cout << "0\n";
    return;
  }
  string te = "123";
  cout << findSubString(s, te).length() << "\n";
  return;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int tests = 1;
  cin >> tests;
  while (tests--) solve();
  return 0;
}
