#include <bits/stdc++.h>
using namespace std;
int wind(string s, string t) {
  vector<int> h(128, 0);
  for (char c : t) h[c]++;
  long long rem = t.length();
  long long l = 0, r = 0, minStart = 0, minLen = numeric_limits<int>::max();
  while (r < s.length()) {
    if (--h[s[r++]] >= 0) rem--;
    while (rem == 0) {
      if (r - l < minLen) {
        minLen = r - l;
        minStart = l;
      }
      if (++h[s[l++]] > 0) rem++;
    }
  }
  return (minLen < numeric_limits<int>::max() ? s.substr(minStart, minLen) : "")
      .length();
}
int main() {
  long long t;
  cin >> t;
  string s;
  while (t--) {
    cin >> s;
    cout << wind(s, "123") << endl;
  }
}
