#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t;
int prf[4][N];
bool valid(int L, int R) {
  int ones = prf[1][R] - prf[1][L - 1];
  int twos = prf[2][R] - prf[2][L - 1];
  int threes = prf[3][R] - prf[3][L - 1];
  if (ones > 0 && twos > 0 && threes > 0) return true;
  return false;
}
int main() {
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    s = ' ' + s;
    int n = s.size();
    for (int i = 1; i <= s.size(); i++) {
      prf[1][i] = prf[1][i - 1] + (s[i] == '1');
      prf[2][i] = prf[2][i - 1] + (s[i] == '2');
      prf[3][i] = prf[3][i - 1] + (s[i] == '3');
    }
    int ans = 1e9;
    for (int i = 1; i <= n - 2; i++) {
      int l = i;
      int r = n;
      while (l < r) {
        int mid = (l + r) / 2;
        if (valid(i, mid)) {
          r = mid;
        } else {
          l = mid + 1;
        }
      }
      if (valid(i, r)) ans = min(ans, r - i + 1);
    }
    if (ans == 1e9) {
      cout << "0\n";
    } else {
      cout << ans << "\n";
    }
  }
}
