#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    string s;
    cin >> s;
    int start = -1;
    int end = -1;
    int ans = INT_MAX;
    for (int i = 0; i < s.length(); ++i) {
      start = i;
      while (i <= s.length() - 1 && s[i] == s[start]) {
        i++;
      }
      end = i - 1;
      i--;
      if (start - 1 >= 0 && end + 1 <= s.length() - 1 &&
          s[start - 1] != s[end + 1]) {
        ans = min(ans, end - start + 3);
      }
    }
    if (ans == INT_MAX) ans = 0;
    cout << ans << endl;
  }
  return 0;
}
