#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int a = 2e5, b = 2e5, c = 2e5, x = 2e5, c1 = 0, c2 = 0, c3 = 0, i;
    for (i = 0; i < s.size(); i++) {
      if (s[i] == '1') {
        a = i;
        c1++;
      }
      if (s[i] == '2') {
        b = i;
        c2++;
      }
      if (s[i] == '3') {
        c = i;
        c3++;
      }
      x = min(x, max(a, max(b, c)) - min(a, min(b, c)) + 1);
    }
    if (c1 == 0 || c2 == 0 || c3 == 0) {
      cout << 0 << '\n';
    } else {
      cout << x << '\n';
    }
  }
}
