#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long arr[4] = {0};
    arr[1] = -1;
    arr[2] = -1;
    arr[3] = -1;
    bool ar[3] = {0};
    long long c = 99999999;
    for (long long i = 0; i < s.size(); i++) {
      if (s[i] == '1' || s[i] == '2' || s[i] == '3') {
        arr[s[i] - '0'] = i;
        ar[s[i] - '0' - 1] = 1;
      }
      if ((ar[0] && ar[1] && ar[2])) {
        long long m = -1;
        long long n = 99999999;
        long long k = 0;
        for (long long i = 1; i < 4; i++) {
          if (arr[i] > m) m = arr[i];
          if (arr[i] < n) n = arr[i];
        }
        if (arr[1] == -1 || arr[2] == -1 || arr[3] == -1)
          k = 0;
        else {
          k = (m - n + 1);
        }
        if (k < c) c = k;
        if (k > 0) ar[s[n] - '0' - 1] = 0;
      }
    }
    if (c == 99999999)
      cout << 0 << endl;
    else
      cout << c << endl;
  }
  return 0;
}
