#include <bits/stdc++.h>
using namespace std;
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
string tb(int x) {
  string ans;
  int c = x;
  for (int i = log2(x); i >= 0; --i) {
    if (pow(2, i) <= c) {
      ans += "1";
      c -= pow(2, i);
    } else {
      ans += "0";
    }
  }
  return ans;
}
bool ispr(int x) {
  if (x <= 1) {
    return false;
  }
  for (int i = 2; i <= sqrt(x); ++i) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const long long INF = 1e18;
void solve() {
  string seq;
  cin >> seq;
  int hi = 0;
  int lo = 0;
  int one = 0;
  int two = 0;
  int three = 0;
  int ans = 999999;
  while (hi <= (int)seq.size()) {
    if (one == 0 || two == 0 || three == 0) {
      if (seq[hi] == '1') {
        one++;
      }
      if (seq[hi] == '2') {
        two++;
      }
      if (seq[hi] == '3') {
        three++;
      }
      ++hi;
    } else {
      ans = min(ans, hi - lo);
      if (seq[lo] == '1') {
        one--;
      }
      if (seq[lo] == '2') {
        two--;
      }
      if (seq[lo] == '3') {
        three--;
      }
      lo++;
    }
  }
  cout << (ans == 999999 ? 0 : ans);
}
int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  bool isT = 1;
  if (isT) {
    int t;
    cin >> t;
    while (t--) {
      solve();
      cout << "\n";
    }
    return 0;
  }
  solve();
  cout << "\n";
  return 0;
}
