#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  cin >> test;
  while (test--) {
    string s;
    cin >> s;
    int a = -1, b = -1, c = -1;
    int ans = s.size();
    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) {
        int x = min(a, min(b, c));
        int y = max(a, max(b, c));
        ans = min(ans, y - x + 1);
      }
    }
    if (a != -1 && b != -1 && c != -1)
      cout << ans << "\n";
    else
      cout << 0 << "\n";
  }
  return 0;
}
