#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, n, m, k, x, g, z, q, t, sum, y, mini, f, e[4], ras, p, l, r;
  cin >> t;
  string s;
  char c;
  for (j = 0; j < t; j++) {
    cin >> s;
    p = s.size();
    ras = p + 1;
    e[3] = 0;
    e[1] = 0;
    e[2] = 0;
    l = 0;
    r = 0;
    c = s[0];
    x = c - 48;
    e[x] = 1;
    while (l <= r) {
      while (r < p) {
        if (e[1] > 0 and e[2] > 0 and e[3] > 0) {
          ras = min(ras, r - l + 1);
          break;
        }
        r = r + 1;
        if (r == p) {
          break;
        }
        c = s[r];
        x = c - 48;
        e[x] = e[x] + 1;
      }
      c = s[l];
      x = c - 48;
      e[x] = e[x] - 1;
      l = l + 1;
    }
    if (ras == p + 1) {
      ras = 0;
    }
    cout << ras << endl;
  }
  return 0;
}
