#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    map<long long, long long> m;
    long long i, j, a = LONG_LONG_MAX, x, y;
    for (i = 0; i < s.size(); i++) {
      m[s[i] - '1'] = i;
      if (m.size() == 3) {
        x = min(m[0], min(m[1], m[2]));
        y = max(m[0], max(m[1], m[2]));
        a = min(a, y - x + 1);
      }
    }
    if (a == LONG_LONG_MAX)
      cout << "0" << endl;
    else
      cout << a << endl;
  }
}
