#include <bits/stdc++.h>
using namespace std;
long long arr[2000005];
void solve() {
  long long a = -1, b = -1, c = -1;
  string s;
  cin >> s;
  long long ans = INT_MAX;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      a = i;
    } else if (s[i] == '2') {
      b = i;
    } else {
      c = i;
    }
    if (a != -1 && b != -1 && c != -1) {
      long long x = min(a, min(b, c));
      long long y = max(a, max(b, c));
      ans = min(ans, y - x + 1);
    }
  }
  if (ans == INT_MAX)
    cout << "0" << endl;
  else
    cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
