#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool f = true;
  string res = "{";
  for (long long i = (0); i <= (static_cast<long long>(v.size()) - 1); i++) {
    if (!f) res += ", ";
    res += to_string(v[i]);
    f = false;
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (auto& a : v) {
    res += (char)('0' + a);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool f = true;
  string res = "{";
  for (auto& x : v) {
    if (!f) res += ", ";
    res += to_string(x);
    f = false;
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> t) {
  return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " +
         to_string(get<2>(t)) + ")";
}
void DBG() { cerr << endl; }
template <typename H, typename... T>
void DBG(H h, T... t) {
  cerr << " " << to_string(h);
  DBG(t...);
}
template <typename T>
void re(T& x) {
  cin >> x;
};
template <typename H, typename... T>
void re(H& h, T&... t) {
  re(h);
  re(t...);
}
template <typename T>
void pr(T x) {
  cout << x;
};
template <typename H, typename... T>
void pr(H h, T... t) {
  pr(h);
  pr(t...);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long tt;
  re(tt);
  while (tt--) {
    string s;
    re(s);
    long long n = (long long)s.size();
    long long a, b, c;
    a = b = c = -1;
    long long inf = (long long)9e18;
    long long ans = inf;
    for (long long i = (0); i <= (n - 1); i++) {
      if (s[i] == '1') {
        a = i;
        if (b == -1 || c == -1) continue;
        ans = min(ans, i - min(b, c) + 1);
      }
      if (s[i] == '2') {
        b = i;
        if (a == -1 || c == -1) continue;
        ans = min(ans, i - min(a, c) + 1);
      }
      if (s[i] == '3') {
        c = i;
        if (b == -1 || a == -1) continue;
        ans = min(ans, i - min(b, a) + 1);
      }
    }
    pr((ans == inf ? 0 : ans), '\n');
  }
  return 0;
}
