#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, j, t, a, b, m, n, c, d, y, x, k, s, r, temp;
  t = 1;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    vector<pair<char, long>> ans;
    map<char, long> mp;
    set<long> st;
    for (i = 0; i < s.length(); i++) {
      mp[s[i]]++;
      st.insert(s[i]);
    }
    if (st.size() != 3) {
      cout << 0 << endl;
      continue;
    }
    char aa = s[0];
    long count = 1;
    for (i = 1; i < s.length(); i++) {
      if (s[i] == aa) {
        count++;
      } else {
        aa = s[i];
        ans.push_back({s[i - 1], count});
        count = 1;
      }
    }
    ans.push_back({s[i - 1], count});
    long sum = LONG_MAX;
    for (i = 1; i < ans.size() - 1; i++) {
      if (ans[i].second + 2 < sum and ans[i - 1].first != ans[i].first and
          ans[i - 1].first != ans[i + 1].first and
          ans[i].first != ans[i + 1].first)
        sum = ans[i].second + 2;
    }
    cout << sum << endl;
  }
}
