#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long tt;
  cin >> tt;
  while (tt--) {
    string str;
    cin >> str;
    long long len = str.length();
    long long mark[4] = {0, 0, 0, 0};
    long long mx = 10000000002;
    long long temp = 0;
    long long cnt = 0;
    long long ch = str[0];
    int startI = -1;
    for (int i = 0; i < len; i++) {
      if (str[i] != str[0]) {
        startI = i - 1;
        break;
      }
    }
    if (startI == -1) {
      cout << 0 << "\n";
      continue;
    }
    int s = 0, e = 0;
    for (int i = startI, j = startI; i < len; i++) {
      if (str[i] == ch) {
        continue;
      }
      ch = str[i];
      int d1 = str[i - 1] - '0';
      int d2 = str[i] - '0';
      if (mark[d1] == 0) {
        mark[d1] = 1;
        cnt++;
      }
      if (mark[d2] == 0) {
        mark[d2] = 1;
        cnt++;
        if (cnt == 2) continue;
      }
      if (cnt == 3) {
        temp = i - j + 1;
        mx = min(mx, temp);
        int x = str[j] - '0';
        mark[x] = 0;
        cnt = 2;
      }
      j = i - 1;
    }
    if (mx != 10000000002) {
      cout << mx << "\n";
    } else
      cout << 0 << "\n";
  }
  return 0;
}
