#include <bits/stdc++.h>
using namespace std;
long long min(long long a, long long b) {
  if (a < b) return a;
  return b;
}
long long max(long long a, long long b) {
  if (a > b) return a;
  return b;
}
void solve2() {
  string s;
  cin >> s;
  long long n = s.length();
  map<char, long long> mp;
  if (n < 3) {
    cout << "0" << endl;
    return;
  }
  mp[s[0]]++;
  mp[s[1]]++;
  mp[s[2]]++;
  if (mp['1'] == 1 && mp['2'] == 1 && mp['3'] == 1) {
    cout << 3 << endl;
    return;
  } else {
    long long j = 0, mn = INT_MAX;
    for (long long i = 3; i < n; i++) {
      mp[s[i]]++;
      if (mp['1'] > 0 && mp['2'] > 0 && mp['3'] > 0) {
        while (j < i && mp['1'] > 0 && mp['2'] > 0 && mp['3'] > 0) {
          mp[s[j]]--;
          j++;
        }
        j--;
        mp[s[j]]++;
        mn = min(mn, i - j + 1);
      }
    }
    if (mn == INT_MAX)
      cout << 0 << endl;
    else
      cout << mn << endl;
  }
}
int main() {
  long long t;
  cin >> t;
  while (t--) solve2();
}
