#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long a = 0, b = 0;
    long long mini = 200001;
    long long x = 0, y = 0, z = 0;
    while (a < s.length() && b < s.length()) {
      if (s[b] == '1') {
        x++;
      } else if (s[b] == '2') {
        y++;
      } else {
        z++;
      }
      b++;
      while (x > 0 && y > 0 && z > 0) {
        if (b - a <= mini) {
          mini = b - a;
        }
        if (s[a] == '1') {
          x--;
        } else if (s[a] == '2') {
          y--;
        } else {
          z--;
        }
        a++;
      }
    }
    if (mini == 200001) {
      cout << 0 << endl;
    } else {
      cout << mini << endl;
    }
  }
  return 0;
}
