#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.length();
    int in1 = -1, in2 = -1, in3 = -1;
    int m = n;
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      if (s[i] == '1') in1 = i;
      if (s[i] == '2') in2 = i;
      if (s[i] == '3') in3 = i;
      if (in1 != -1 && in2 != -1 && in3 != -1) {
        a = min(in1, in2);
        b = min(a, in3);
        c = max(in1, in2);
        d = max(c, in3);
        m = min(m, (d - b));
      }
    }
    if (in1 != -1 && in2 != -1 && in3 != -1)
      cout << m + 1 << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}
