#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.length();
    int ans = n + 1;
    int one = -1, two = -1, three = -1;
    for (int i = 0; i <= n; ++i) {
      if (i < n) {
        if (s[i] == '1')
          one = i;
        else if (s[i] == '2')
          two = i;
        else if (s[i] == '3')
          three = i;
      }
      if (one != -1 && two != -1 && three != -1)
        ans = min(ans, i - min(one, min(two, three)));
    }
    if (ans == n + 1)
      cout << "0\n";
    else
      cout << ans + 1 << '\n';
  }
}
