#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    string s;
    cin >> s;
    int l1 = -1, l2 = -1, l3 = -1, ans = 200100;
    for (int i = 0; i < s.size(); ++i) {
      int x = s[i];
      if (x == 49) {
        l1 = i;
      }
      if (x == 50) {
        l2 = i;
      }
      if (x == 51) {
        l3 = i;
      }
      if (l1 != -1 && l2 != -1 && l3 != -1) {
        ans = min(ans, max({l1, l2, l3}) - min({l1, l2, l3}));
      }
    }
    if (ans == 200100) {
      cout << 0 << endl;
    } else
      cout << ans + 1 << endl;
  }
  return 0;
}
