#include <bits/stdc++.h>
using namespace std;
void sorted(vector<long long int> &arr) { sort(arr.begin(), arr.end()); }
void coutarr(vector<long long int> arr) {
  long long int n = arr.size();
  for (long long int i = 0; i < n; i++) cout << arr[i] << " ";
  cout << endl;
}
void coutarr2(vector<vector<long long int>> arr) {
  long long int n = arr.size();
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < arr[i].size(); j++) cout << arr[i][j] << " ";
    cout << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int u;
  cin >> u;
  while (u--) {
    long long int ans = INT_MAX;
    string s;
    cin >> s;
    long long int n = s.size();
    long long int one = 0, two = 0, three = 0, one_l = -1, two_l = -1,
                  three_l = -1;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '1') {
        one = 1;
        one_l = i;
      } else if (s[i] == '2') {
        two = 1;
        two_l = i;
      } else {
        three_l = i;
        three = 1;
      }
      if (one == 1 and two == 1 and three == 1)
        ans = min(ans, max(one_l, max(three_l, two_l)) -
                           min(one_l, min(three_l, two_l)) + 1);
    }
    if (ans == INT_MAX)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
