#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  cin >> s;
  int res = 0;
  char tmp = s[0];
  int left = -1;
  int i = 0;
  int si = s.size();
  int tol = 0;
  int ANS = 2e5 + 14;
  while (i < si) {
    if (s[i] == tmp) {
      ++tol;
      ++i;
    } else {
      set<char> seen;
      seen.insert(tmp);
      if (i < si) seen.insert(s[i]);
      if (left != -1) seen.insert(s[left]);
      if (seen.size() == 3) {
        ANS = min(tol + 2, ANS);
      }
      if (i < si) tmp = s[i];
      left = i - 1;
      tol = 0;
    }
  };
  if (ANS == 2e5 + 14)
    cout << "0\n";
  else
    cout << ANS << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int q;
  cin >> q;
  for (; q > 0; --q) solve();
  return 0;
}
