#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 9;
string s;
int cnt[3];
bool isValid() {
  if (cnt[0] > 0 && cnt[1] > 0 && cnt[2] > 0) return true;
  return false;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> s;
    if (s.size() < 3) {
      cout << 0 << endl;
      continue;
    }
    memset(cnt, 0, sizeof(cnt));
    int ans = INF;
    int pt1 = 0;
    int pt2 = 0;
    while (pt2 < s.size()) {
      while (pt2 < s.size() && !isValid()) {
        cnt[s[pt2] - '1']++;
        pt2++;
      }
      if (!isValid()) break;
      ans = min(ans, pt2 - pt1);
      while (pt1 <= pt2 && pt1 < s.size() && isValid()) {
        ans = min(ans, pt2 - pt1);
        cnt[s[pt1] - '1']--;
        pt1++;
      }
    }
    if (ans == INF)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
