#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9;
const long long linf = 1e18;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
string ds = "RLDU";
const int MAX = 1e5;
string to_string(char z) { return string(1, z); }
string to_string(bool a) { return a ? "true" : "false"; }
string to_string(const char* st) { return string(st); }
string to_string(string st) { return st; }
string to_string(vector<bool> vec) {
  {
    string res;
    for (int i = (0); i < (((int)vec.size())); i++) res += char('0' + vec[i]);
    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> p) {
  return "[" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + "]";
}
template <size_t S>
string to_string(bitset<S> bb) {
  string res;
  for (int i = (0); i < (S); i++) res += char('0' + bb[i]);
  return res;
}
template <class T>
string to_string(T v) {
  bool f = 1;
  string an;
  for (auto& x : v) {
    if (!f) an += ' ';
    f = 0;
    an += to_string(x);
  }
  return an;
}
void _Debug() { cout << "]" << endl; }
template <class H, class... T>
void _Debug(H h, T... t) {
  cout << to_string(h);
  if (sizeof...(t)) cout << ", ";
  _Debug(t...);
}
void solve() {
  string s;
  cin >> s;
  vector<int> one, two, three;
  for (int i = (0); i < (((int)s.size())); i++) {
    if (s[i] == '1')
      one.push_back(i);
    else if (s[i] == '2')
      two.push_back(i);
    else
      three.push_back(i);
  }
  int best = INT_MAX;
  0;
  0;
  0;
  if (((int)one.size()) == 0 || ((int)two.size()) == 0 ||
      ((int)three.size()) == 0) {
    cout << 0 << endl;
    return;
  }
  for (int i = (0); i < (((int)s.size())); i++) {
    if (s[i] == '1') {
      int idx1 = upper_bound(two.begin(), two.end(), i) - two.begin();
      int idx2 = upper_bound(three.begin(), three.end(), i) - three.begin();
      if (idx1 == ((int)two.size()) || idx2 == ((int)three.size())) continue;
      best = min(best, max(two[idx1], three[idx2]) - i + 1);
    } else if (s[i] == '2') {
      int idx1 = upper_bound(one.begin(), one.end(), i) - one.begin();
      int idx2 = upper_bound(three.begin(), three.end(), i) - three.begin();
      if (idx1 == ((int)one.size()) || idx2 == ((int)three.size())) continue;
      best = min(best, max(one[idx1], three[idx2]) - i + 1);
    } else {
      int idx1 = upper_bound(two.begin(), two.end(), i) - two.begin();
      int idx2 = upper_bound(one.begin(), one.end(), i) - one.begin();
      if (idx1 == ((int)two.size()) || idx2 == ((int)one.size())) continue;
      best = min(best, max(two[idx1], one[idx2]) - i + 1);
    }
  }
  cout << best << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(6);
  int t = 1;
  cin >> t;
  for (int i = (1); i < (t + 1); i++) {
    solve();
  }
}
