#include <bits/stdc++.h>
using namespace std;
void loop() {
  string str;
  cin >> str;
  int n = str.size();
  int i = 0, j = 0;
  int ans = INT_MAX;
  set<int> s;
  map<int, int> mp;
  while (i < n && j < n) {
    while (s.size() < 3 && i < n && j < n) {
      s.insert(str[j] - '0');
      ++mp[str[j] - '0'];
      ++j;
    }
    if (s.size() == 3) ans = min(ans, j - i);
    while (s.size() == 3) {
      ans = min(ans, j - i);
      --mp[str[i] - '0'];
      if (mp[str[i] - '0'] == 0) {
        s.erase(str[i] - '0');
      }
      ++i;
    }
  }
  if (ans == INT_MAX)
    cout << "0\n";
  else
    cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int myTest;
  cin >> myTest;
  while (myTest--) {
    loop();
  }
}
