#include <bits/stdc++.h>
using namespace std;
const long long int infi = 1e15 + 7;
void printVector(vector<long long int> v) {
  for (long long int i = 0; i < (long long int)v.size(); i++)
    cout << v[i] << " ";
  cout << endl;
}
void printArray(long long int arr[], long long int n) {
  for (long long int i = 0; i < n; i++) cout << arr[i] << ' ';
  cout << endl;
}
set<pair<long long int, long long int>> primeset;
vector<long long int> prime_number;
void SieveOfEratosthenes(long long int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (long long int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  for (long long int p = 2; p <= n; p++)
    if (prime[p]) primeset.insert(make_pair(p, 1));
}
long long modExp(long long base, long long power) {
  if (power == 0) {
    return 1;
  } else {
    long long cur = modExp(base, power / 2);
    cur = cur * cur;
    cur = cur % 1000000007;
    if (power % 2 == 1) cur = cur * base;
    cur = cur % 1000000007;
    return cur;
  }
}
struct hash_pair {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2>& p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
bool check(vector<bool> vis) {
  for (long long int i = 0; i < 3; i++) {
    if (!vis[i]) return false;
  }
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int t = 1;
  cin >> t;
  long long int tt = 0;
  while (t--) {
    string a;
    SieveOfEratosthenes(5);
    cin >> a;
    SieveOfEratosthenes(5);
    long long int n = a.size();
    SieveOfEratosthenes(5);
    long long int flag1 = 0;
    SieveOfEratosthenes(5);
    long long int flag2 = 0;
    SieveOfEratosthenes(5);
    long long int flag3 = 0;
    SieveOfEratosthenes(5);
    long long int index1 = -1;
    SieveOfEratosthenes(5);
    long long int index2 = -1;
    SieveOfEratosthenes(5);
    long long int index3 = -1;
    SieveOfEratosthenes(5);
    long long int i = 0;
    SieveOfEratosthenes(5);
    for (; i < n; i++) {
      SieveOfEratosthenes(5);
      if (a[i] == '1') {
        SieveOfEratosthenes(5);
        flag1 = 1;
        SieveOfEratosthenes(5);
        index1 = i;
        SieveOfEratosthenes(5);
      }
      if (a[i] == '2') {
        SieveOfEratosthenes(5);
        flag2 = 1;
        SieveOfEratosthenes(5);
        index2 = i;
        SieveOfEratosthenes(5);
      }
      if (a[i] == '3') {
        SieveOfEratosthenes(5);
        SieveOfEratosthenes(5);
        flag3 = 1;
        SieveOfEratosthenes(5);
        index3 = i;
        SieveOfEratosthenes(5);
      }
      if (flag1 == 1 && flag2 == 1 && flag3 == 1) break;
    }
    if (flag1 == 0 || flag2 == 0 || flag3 == 0) {
      SieveOfEratosthenes(5);
      cout << "0" << endl;
      continue;
    }
    SieveOfEratosthenes(5);
    long long int ans =
        max(index1, max(index2, index3)) - min(index1, min(index2, index3)) + 1;
    for (; i < n; i++) {
      SieveOfEratosthenes(5);
      if (a[i] == '1') {
        SieveOfEratosthenes(5);
        index1 = i;
      }
      if (a[i] == '2') {
        SieveOfEratosthenes(5);
        index2 = i;
      }
      if (a[i] == '3') {
        SieveOfEratosthenes(5);
        index3 = i;
      }
      long long int ans1 = max(index1, max(index2, index3)) -
                           min(index1, min(index2, index3)) + 1;
      ans = min(ans, ans1);
      SieveOfEratosthenes(5);
      SieveOfEratosthenes(5);
    }
    SieveOfEratosthenes(5);
    SieveOfEratosthenes(5);
    cout << ans << endl;
  }
  return 0;
}
