#include <bits/stdc++.h>
using namespace std;
string str;
int n, len, pos_a, pos_b, pos_c;
int a, b, mx, mn, k, print;
int main() {
  cin >> n;
  while (n--) {
    cin >> str;
    pos_a = pos_b = pos_c = 0;
    print = 200001;
    len = str.size();
    for (auto i = 0; i < len; i++) {
      if (str[i] == '1')
        pos_a = i + 1;
      else if (str[i] == '2')
        pos_b = i + 1;
      else if (str[i] == '3')
        pos_c = i + 1;
      if (pos_a != 0 && pos_b != 0 && pos_c != 0) {
        a = max(pos_a, pos_b);
        mx = max(a, pos_c);
        b = min(pos_a, pos_b);
        mn = min(b, pos_c);
        k = mx - mn + 1;
        if (print > k) print = k;
      }
    }
    if (print == 200001)
      cout << "0\n";
    else
      cout << print << '\n';
  }
  return 0;
}
