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