#include <bits/stdc++.h>
using namespace std;
int t;
string s;
int main() {
  cin >> t;
  while (t--) {
    cin >> s;
    int n = s.size();
    int ans = n + 1, l = 0, c[4] = {0};
    for (int i = 0; i < n; i++) {
      c[s[i] - '0']++;
      while (c[1] > 0 && c[2] > 0 && c[3] > 0) {
        ans = min(i - l + 1, ans);
        c[s[l++] - '0']--;
      }
    }
    if (ans == n + 1)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
}
