#include <bits/stdc++.h>
using namespace std;
long long p = 1e9 + 7;
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  };
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long f1 = 0, f2 = 0, f3 = 0;
    for (long long i = 0; i < s.length(); i++) {
      if (s[i] == '1') {
        f1 = 1;
      }
      if (s[i] == '2') {
        f2 = 1;
      }
      if (s[i] == '3') {
        f3 = 1;
      }
    }
    if (f1 != 1 || f2 != 1 || f3 != 1) {
      cout << "0" << endl;
    } else {
      vector<long long> v, v1;
      long long ff1 = 0, ff2 = 0, ff3 = 0;
      for (int i = 0; i < s.length(); i++) {
        if (s[i] == '1') {
          ff1++;
          if (ff2 > 0) {
            v.push_back(ff2);
            v1.push_back(2);
            ff2 = 0;
          }
          if (ff3 > 0) {
            v.push_back(ff3);
            v1.push_back(3);
            ff3 = 0;
          }
        }
        if (s[i] == '2') {
          ff2++;
          if (ff1 > 0) {
            v.push_back(ff1);
            v1.push_back(1);
            ff1 = 0;
          }
          if (ff3 > 0) {
            v.push_back(ff3);
            v1.push_back(3);
            ff3 = 0;
          }
        }
        if (s[i] == '3') {
          ff3++;
          if (ff1 > 0) {
            v.push_back(ff1);
            v1.push_back(1);
            ff1 = 0;
          }
          if (ff2 > 0) {
            v.push_back(ff2);
            v1.push_back(2);
            ff2 = 0;
          }
        }
        if (i == (s.length() - 1)) {
          if (ff1 > 0) {
            v.push_back(ff1);
            v1.push_back(1);
          }
          if (ff2 > 0) {
            v.push_back(ff2);
            v1.push_back(2);
          }
          if (ff3 > 0) {
            v.push_back(ff3);
            v1.push_back(3);
          }
        }
      }
      long long sum = INT_MAX;
      for (int i = 0; i < (v.size() - 2); i++) {
        if (v1[i] != v1[i + 2]) sum = min(sum, 2 + v[i + 1]);
      }
      cout << sum << endl;
    }
  }
  return 0;
}
