#include <bits/stdc++.h>
using namespace std;
const int INT_INF = 1e9;
const long long LL_INF = 1e18;
int getInt(char c) { return c - '0'; }
int getDistance(int index, int y, int z, vector<vector<int>>& last) {
  int p = last[index][y];
  if (p == -1) return INT_INF;
  int q = last[p][z];
  if (q == -1) return INT_INF;
  return index - q + 1;
}
int main() {
  int tt;
  cin >> tt;
  while (tt--) {
    string s;
    cin >> s;
    int n = (int)s.size();
    vector<vector<int>> last(n, vector<int>(3, -1));
    last[0][getInt(s[0]) - 1] = 0;
    int ans = INT_INF;
    for (int i = 1; i < n; i++) {
      int u = getInt(s[i]) - 1;
      for (int j = 0; j < 3; j++) {
        if (u == j)
          last[i][u] = i;
        else
          last[i][j] = last[i - 1][j];
      }
      if (u == 0) {
        ans = min(ans, getDistance(i, 1, 2, last));
        ans = min(ans, getDistance(i, 2, 1, last));
      } else if (u == 1) {
        ans = min(ans, getDistance(i, 0, 2, last));
        ans = min(ans, getDistance(i, 2, 0, last));
      } else {
        ans = min(ans, getDistance(i, 0, 1, last));
        ans = min(ans, getDistance(i, 1, 0, last));
      }
    }
    cout << (ans == INT_INF ? 0 : ans) << endl;
  }
  return 0;
}
