#include <bits/stdc++.h>
using namespace std;
void solve() {
  string strr;
  cin >> strr;
  map<char, int> my_map_for_string;
  int check = 0, ans = INT_MAX;
  for (int i = 0, itr_j = 0; i < strr.size(); i++) {
    my_map_for_string[strr[i]]++;
    if (my_map_for_string[strr[i]] == 1) check++;
    while (itr_j <= i && my_map_for_string[strr[itr_j]] > 1) {
      my_map_for_string[strr[itr_j]]--;
      itr_j++;
    }
    if (check == 3) ans = min(ans, i - itr_j + 1);
  }
  if (ans == INT_MAX) ans = 0;
  cout << ans << endl;
}
int main() {
  int testcase;
  cin >> testcase;
  while (testcase--) {
    solve();
  }
}
