#include <bits/stdc++.h>
using namespace std;
long long n, z, x, s;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    string ss;
    cin >> ss;
    long long s = 1000000000001;
    long long x1 = 0, x2 = 0, x3 = 0;
    for (int i = 0; i < ss.size(); i++) {
      if (ss[i] == '1') x1 = i + 1;
      if (ss[i] == '2') x2 = i + 1;
      if (ss[i] == '3') x3 = i + 1;
      if (x1 != 0 && x2 != 0 && x3 != 0)
        s = min(s, max({x1, x2, x3}) - min({x1, x2, x3}));
    }
    if (s == 1000000000001)
      cout << 0 << "\n";
    else
      cout << s + 1 << "\n";
  }
}
