#include <bits/stdc++.h>
using namespace std;
long long getmul(long long n) {
  int m1 = 10, m2 = 0;
  while (n > 0) {
    int k = n % 10;
    m2 = max(m2, k);
    m1 = min(m1, k);
    n = n / 10;
  }
  return m1 * m2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int ans = INT_MAX;
    long long int pos1 = -1, pos2 = -1, pos3 = -1;
    for (int i = 0; i < ((int)(s).size()); i++) {
      if (s[i] == '1') pos1 = i;
      if (s[i] == '2') pos2 = i;
      if (s[i] == '3') pos3 = i;
      if (pos1 != -1 && pos2 != -1 && pos3 != -1) {
        long long int a = max(pos1, max(pos2, pos3));
        long long int b = min(pos1, min(pos2, pos3));
        ans = min(ans, a - b + 1);
      }
    }
    if (pos1 == -1 || pos2 == -1 || pos3 == -1)
      cout << "0"
           << "\n";
    else
      cout << ans << "\n";
  }
  return 0;
}
