#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    string str;
    cin >> str;
    int minn = INT_MAX;
    map<char, int> mp;
    mp.clear();
    int left = 0, right = 0;
    while (1) {
      mp[str[right]]++;
      if (right == str.size() - 1 && mp.size() != 3) break;
      if (right == str.size() - 1 && mp.size() == 3) {
        minn = min(minn, right - left + 1);
        while (1) {
          mp[str[left]]--;
          if (mp[str[left]] == 0) {
            minn = min(minn, right - left + 1);
            mp.erase(str[left]);
            left++;
            break;
          }
          left++;
        }
        break;
      }
      if (mp.size() == 3) {
        minn = min(minn, right - left + 1);
        while (1) {
          mp[str[left]]--;
          if (mp[str[left]] == 0) {
            minn = min(minn, right - left + 1);
            mp.erase(str[left]);
            left++;
            break;
          }
          left++;
        }
      }
      right++;
    }
    if (minn == INT_MAX)
      cout << 0 << "\n";
    else
      cout << minn << "\n";
  }
}
