#include <bits/stdc++.h>
using namespace std;
const long long int MAXN = 2 * 1e5 + 10;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    vector<long long int> v;
    long long int i;
    long long int p = -1, q = -1, r = -1;
    for (i = 0; i < s.length(); i++) {
      if (s[i] == '1') p = i;
      if (s[i] == '2') q = i;
      if (s[i] == '3') r = i;
      if (p == -1 || q == -1 || r == -1)
        continue;
      else {
        v.push_back(max(abs(p - q), max(abs(p - r), abs(q - r))));
      }
    }
    long long int mini = 2 * 1e9;
    ;
    if (v.size() == 0)
      cout << 0 << endl;
    else {
      for (i = 0; i < v.size(); i++) mini = min(mini, v[i]);
      cout << mini + 1 << endl;
    }
  }
  return 0;
}
