#include <bits/stdc++.h>
using namespace std;
int solve() {
  string s;
  cin >> s;
  int n = s.length();
  vector<int> counts(3);
  int i = 0;
  int j = 0;
  int res = 0;
  auto check = [&]() -> bool {
    return counts[0] != 0 && counts[1] != 0 && counts[2] != 0;
  };
  while (j < n) {
    while (j < n && !check()) {
      int a = s[j] - '1';
      counts[a]++;
      j++;
    }
    while (i < n && check()) {
      int a = s[i] - '1';
      counts[a]--;
      i++;
    }
    int a = s[i - 1] - '1';
    counts[a]++;
    if (check()) {
      if (res == 0) {
        res = j - i + 1;
      } else {
        res = min(res, j - i + 1);
      }
    }
    counts[a]--;
  }
  return res;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    cout << solve() << "\n";
  }
}
