#include<string>
#include<vector>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main(){
int t; cin >> t;
for (int i = 0; i < t; i++) {
string s;cin >> s;
int ans = int(1e9);
int n = s.size();
// zhi len
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++; // 相同 len++
}
int m = c.size();
for (int i = 1; i < m - 1; i++)
if (c[i - 1].first != c[i + 1].first)//3项不同
ans = min(ans, c[i].second + 2);
if (ans > n) ans = 0;
cout << ans << "\n";
}
}