#include <bits/stdc++.h>
using namespace std;
bool isValid(string s, int mid) {
  int one = 0, two = 0, three = 0;
  for (int i = 0; i < mid; i++) {
    if (s.at(i) == '1') one++;
    if (s.at(i) == '2') two++;
    if (s.at(i) == '3') three++;
  }
  if (one > 0 && two > 0 && three > 0) return true;
  for (int i = mid; i < s.length(); i++) {
    if (s.at(i) == '1') one++;
    if (s.at(i) == '2') two++;
    if (s.at(i) == '3') three++;
    if (s.at(i - mid) == '1') one--;
    if (s.at(i - mid) == '2') two--;
    if (s.at(i - mid) == '3') three--;
    if (one > 0 && two > 0 && three > 0) return true;
  }
  return false;
}
int main() {
  int t;
  cin >> t;
  while (t-- > 0) {
    string s;
    cin >> s;
    int n = s.length();
    int ans = 200001;
    if (n <= 2)
      cout << "0" << endl;
    else {
      int low = 3, high = n;
      while (low <= high) {
        int mid = (low + high) / 2;
        if (isValid(s, mid)) {
          ans = min(ans, mid);
          high = mid - 1;
        } else
          low = mid + 1;
      }
      if (ans <= n)
        cout << ans << endl;
      else
        cout << "0" << endl;
    }
  }
}
