#include <bits/stdc++.h>
using namespace std;
void solve1() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long timesleepafterfirst = c - d;
  long long timerequired = a - b;
  if (timerequired < 1) {
    cout << b << endl;
    return;
  }
  if (timesleepafterfirst > 0) {
    long long ce = ceil(timerequired / (timesleepafterfirst * 1.0));
    cout << (b + (ce * c)) << endl;
    return;
  }
  cout << -1 << endl;
}
void solve2() {
  string s;
  cin >> s;
  int ans = INT_MAX;
  int n = s.size();
  vector<pair<char, int>> c;
  for (auto x : s) {
    if (c.empty() || c.back().first != x)
      c.push_back(make_pair(x, 1));
    else
      c.back().second++;
  }
  int m = c.size();
  for (int i = 1; i < m - 1; i++)
    if (c[i - 1].first != c[i + 1].first) ans = min(ans, c[i].second + 2);
  if (ans > n) ans = 0;
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve2();
  }
  return 0;
}
