#include <bits/stdc++.h>
using namespace std;
using namespace std;
string minWindow(string s, string t) {
  vector<long long> hist(128, 0);
  for (char c : t) hist[c]++;
  long long remaining = t.length();
  long long left = 0, right = 0, minStart = 0,
            minLen = numeric_limits<long long>::max();
  while (right < s.length()) {
    if (--hist[s[right++]] >= 0) remaining--;
    while (remaining == 0) {
      if (right - left < minLen) {
        minLen = right - left;
        minStart = left;
      }
      if (++hist[s[left++]] > 0) remaining++;
    }
  }
  return minLen < numeric_limits<long long>::max() ? s.substr(minStart, minLen)
                                                   : "";
}
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long x = s.length();
    string t = "123";
    string ss = minWindow(s, t);
    long long sz = ss.length();
    cout << sz << endl;
  }
  return 0;
}
