#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  for (long long int i = 0; i < v.size() - 1; ++i) os << v[i] << ", ";
  os << v[v.size() - 1] << "]\n";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << "{";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin()) os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v) {
  os << "{";
  for (auto it : v) {
    os << "(" << it.first << " : " << it.second << ")";
    if (it != *v.rbegin()) os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
template <class C>
void mini(C& a4, C b4) {
  a4 = min(a4, b4);
}
template <class C>
void maxi(C& a4, C b4) {
  a4 = max(a4, b4);
}
long long int fpow(long long int x, long long int y, long long int p);
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int T = 1;
  cin >> T;
  for (long long int tc = 1; tc <= T; ++tc) {
    string s;
    cin >> s;
    long long int n = s.length();
    long long int l1[n], r1[n], l2[n], r2[n], l3[n], r3[n];
    long long int p1 = -1, p2 = -1, p3 = -1;
    for (long long int i = 0; i < n; ++i) {
      if (s[i] == '1') p1 = i;
      if (s[i] == '2') p2 = i;
      if (s[i] == '3') p3 = i;
      l1[i] = p1;
      l2[i] = p2;
      l3[i] = p3;
    }
    p1 = n;
    p2 = n;
    p3 = n;
    for (long long int i = n - 1; i >= 0; --i) {
      if (s[i] == '1') p1 = i;
      if (s[i] == '2') p2 = i;
      if (s[i] == '3') p3 = i;
      r1[i] = p1;
      r2[i] = p2;
      r3[i] = p3;
    }
    long long int ans = n;
    for (long long int i = 0; i < n; ++i) {
      if (s[i] == '1') {
        if (l2[i] != -1 && r3[i] != n) ans = min(ans, r3[i] - l2[i] + 1);
        if (l3[i] != -1 && r2[i] != n) ans = min(ans, r2[i] - l3[i] + 1);
      }
      if (s[i] == '2') {
        if (l1[i] != -1 && r3[i] != n) ans = min(ans, r3[i] - l1[i] + 1);
        if (l3[i] != -1 && r1[i] != n) ans = min(ans, r1[i] - l3[i] + 1);
      }
      if (s[i] == '3') {
        if (l1[i] != -1 && r2[i] != n) ans = min(ans, r2[i] - l1[i] + 1);
        if (r1[i] != n && l2[i] != -1) ans = min(ans, r1[i] - l2[i] + 1);
      }
    }
    map<char, long long int> m;
    for (auto i : s) m[i]++;
    if (m.size() != 3) ans = 0;
    cout << ans << endl;
  }
  return 0;
}
long long int fpow(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
