#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t, n;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    n = (int)s.size();
    int a, b, c, d, e, ans = 1e9;
    a = b = c = -1;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        a = i;
      else if (s[i] == '2')
        b = i;
      else
        c = i;
      d = max(a, max(b, c));
      e = min(a, min(b, c));
      if (e >= 0) {
        ans = min(d - e + 1, ans);
      }
    }
    if (ans == 1e9)
      cout << "0"
           << "\n";
    else
      cout << ans << "\n";
  }
  return 0;
}
