#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int one = 9999999, two = 9999999, three = 9999999, mn = 9999999;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '1') {
        one = i;
      } else if (s[i] == '2') {
        two = i;
      } else {
        three = i;
      }
      mn = min(mn, max({one, two, three}) - min({one, two, three}) + 1);
    }
    if (one == 9999999 || two == 9999999 || three == 9999999) {
      cout << "0\n";
    } else {
      cout << mn << "\n";
    }
  }
  return 0;
}
