#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    string s;
    cin >> s;
    int last[4];
    memset(last, 0, sizeof last);
    map<char, int> used;
    last[s[0] - '0'] = 0;
    used[s[0]] = 1;
    int dp[s.size()];
    set<char> num;
    num.insert(s[0]);
    dp[0] = 1;
    int res = INT_MAX;
    for (int i = 1; i < s.size(); i++) {
      num.insert(s[i]);
      dp[i] = num.size();
      used[s[i]] = 1;
      last[s[i] - '0'] = i;
      if (dp[i] == 3 and s[i] != s[i - 1]) {
        if (s[i] == '3') {
          if (s[i - 1] == '2')
            res = min(res, i - last[1] + 1);
          else
            res = min(res, i - last[2] + 1);
        }
        if (s[i] == '2') {
          if (s[i - 1] == '1')
            res = min(res, i - last[3] + 1);
          else
            res = min(res, i - last[1] + 1);
        }
        if (s[i] == '1') {
          if (s[i - 1] == '3')
            res = min(res, i - last[2] + 1);
          else
            res = min(res, i - last[3] + 1);
        }
      }
    }
    if (res == INT_MAX)
      cout << "0\n";
    else
      cout << res << '\n';
  }
  return 0;
}
