#include <bits/stdc++.h>
using namespace std;
vector<long long int> prime, sfac;
vector<long long int> ps;
void sieve(long long int n) {
  prime.assign(n + 1, 1);
  sfac.assign(n + 1, 1);
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == 1) {
      sfac[p] = p;
      for (int i = p * p; i <= n; i += p) {
        prime[i] = 0;
        sfac[i] = p;
      }
    }
  }
  for (long long int i = 2; i <= n; i++) {
    if (prime[i]) ps.push_back(i);
  }
}
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int power(long long int x, long long int y) {
  long long int res = 1;
  while (y > 0) {
    long long int k = y & 1LL;
    if (k) res = res * x;
    y = y >> 1LL;
    x = x * x;
  }
  return res;
}
int isPrime(long long int n) {
  if (n <= 1) return 0;
  if (n <= 3) return 1;
  if (n % 2 == 0 || n % 3 == 0) return 0;
  for (long long int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return 0;
  return 1;
}
int main() {
  long long int tt;
  cin >> tt;
  while (tt--) {
    string s;
    cin >> s;
    int prev = s[0] - 48;
    vector<pair<int, pair<int, int>>> v;
    for (int i = 1; i < s.size(); i++) {
      if (s[i] - 48 == prev) continue;
      v.push_back(make_pair(i - 1, make_pair(prev, s[i] - 48)));
      prev = s[i] - 48;
    }
    if (v.empty()) {
      cout << 0 << endl;
      continue;
    }
    int c1 = v[0].second.first;
    int c2 = v[0].second.second;
    int pos = v[0].first, min = INT_MAX, fl = 0;
    for (int i = 1; i < v.size(); i++) {
      int cc1 = v[i].second.first;
      int cc2 = v[i].second.second;
      if ((c1 == cc1 && c2 == cc2) || (c1 == cc2 && c2 == cc1)) {
        pos = v[i].first;
        continue;
      } else {
        int len = v[i].first - pos + 2;
        fl = 1;
        if (len < min) min = len;
        c1 = cc1;
        c2 = cc2;
        pos = v[i].first;
      }
    }
    if (fl == 0) {
      cout << 0 << endl;
    } else
      cout << min << endl;
  }
}
