#include <bits/stdc++.h>
using namespace std;
int minSub(string input) {
  int firstIndex = 0;
  int secondIndex = 0;
  vector<int> count = {0, 0, 0, 0};
  for (int i = 0; i < input.size(); i++) {
    count[(int)(input[i] - '0')]++;
    if (count[1] >= 1 && count[2] >= 1 && count[3] >= 1) {
      secondIndex = i;
      break;
    }
  }
  int minSize = secondIndex - firstIndex + 1;
  int curSize = secondIndex - firstIndex + 1;
  if (secondIndex == 0) {
    return 0;
  }
  while (secondIndex < input.size()) {
    if (count[1] == 1 && count[2] == 1 && count[3] == 1) {
      return 3;
    } else if (count[1] >= 1 && count[2] >= 1 && count[3] >= 1) {
      count[(int)(input[firstIndex] - '0')]--;
      firstIndex++;
    } else {
      if (secondIndex == input.size() - 1) {
        break;
      }
      count[(int)(input[firstIndex] - '0')]--;
      firstIndex++;
      secondIndex++;
      count[input[secondIndex] - '0']++;
    }
    if (count[1] >= 1 && count[2] >= 1 && count[3] >= 1) {
      curSize = count[1] + count[2] + count[3];
      minSize = min(minSize, curSize);
    }
  }
  return minSize;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tests;
  cin >> tests;
  for (int i = 0; i < tests; i++) {
    string input;
    cin >> input;
    cout << minSub(input) << "\n";
  }
  return 0;
}
