#include <bits/stdc++.h>
using namespace std;
const int MAX_CHARS = 256;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t, i, j, k, temp, temp2, n, m, x, y, z, sum = 0, o;
  cin >> t;
  while (t--) {
    string a, b = "123";
    cin >> a;
    n = a.size();
    bool flag1 = false, flag2 = false, flag3 = false;
    for (i = 0; i < n; i++) {
      if (a[i] == '1') flag1 = true;
      if (a[i] == '2') flag2 = true;
      if (a[i] == '3') flag3 = true;
    }
    if (flag1 == false || flag2 == false || flag3 == false) {
      cout << "0\n";
      continue;
    }
    long long int dist_count = 0;
    bool visited[MAX_CHARS] = {false};
    for (i = 0; i < n; i++) {
      if (visited[a[i]] == false) {
        visited[a[i]] = true;
        dist_count++;
      }
    }
    long long int start = 0, s_index = -1, min_len = LONG_LONG_MAX;
    long long int count = 0;
    long long int curr_count[MAX_CHARS] = {0};
    for (j = 0; j < n; j++) {
      curr_count[a[j]]++;
      if (curr_count[a[j]] == 1) count++;
      if (count == dist_count) {
        while (curr_count[a[start]] > 1) {
          if (curr_count[a[start]] > 1) curr_count[a[start]]--;
          start++;
        }
        long long int len_window = j - start + 1;
        if (min_len > len_window) {
          min_len = len_window;
          s_index = start;
        }
      }
    }
    cout << min_len << endl;
  }
}
