#include <bits/stdc++.h>
using namespace std;
int find(int num, vector<int>& a, int s, int e) {
  if (s > e) return -1;
  if (s == e) {
    if (a[s] > num)
      return a[s];
    else
      return -1;
  }
  if (a[(s + e) / 2] >= num) {
    return find(num, a, s, (s + e) / 2);
  } else
    return find(num, a, (s + e) / 2 + 1, e);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int i, j;
    string s;
    cin >> s;
    vector<int> a1, a2, a3;
    for (i = 0; i < s.length(); i++) {
      if (s[i] == '1')
        a1.push_back(i);
      else if (s[i] == '2')
        a2.push_back(i);
      else
        a3.push_back(i);
    }
    if (a1.size() == 0 && a2.size() == 0 && a3.size() == 0) {
      cout << 0 << endl;
      continue;
    }
    int ans = 1e9;
    int x, y, z;
    x = 0, y = 0, z = 0;
    for (i = 0; i < s.length(); i++) {
      if (x >= a1.size() || y >= a2.size() || z >= a3.size()) break;
      if (a1[x] == i) {
        x++;
        ans = min(ans, max(a2[y], a3[z]) - i + 1);
      } else if (a2[y] == i) {
        y++;
        ans = min(ans, max(a1[x], a3[z]) - i + 1);
      } else {
        z++;
        ans = min(ans, max(a1[x], a2[y]) - i + 1);
      }
    }
    if (ans == 1e9)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
}
