#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    unordered_map<char, int> mp;
    mp['1'] = (-1);
    mp['2'] = (-1);
    mp['3'] = (-1);
    int n = s.length(), len = 200010, curr = 0;
    for (int i = 0; i < n; i++) {
      mp[s[i]] = i;
      if (mp['1'] >= 0 && mp['2'] >= 0 && mp['3'] >= 0) {
        vector<int> v;
        v.push_back(mp['1']);
        v.push_back(mp['2']);
        v.push_back(mp['3']);
        sort(v.begin(), v.end());
        curr = v[2] - v[0] + 1;
        len = min(len, curr);
      }
    }
    if (len == 200010) len = 0;
    cout << len << endl;
  }
  return 0;
}
