for _ in range(int(input())):
s = input()
m = 0
l = [-1]*3
for i in range(len(s)):
l[int(s[i])-1] = i
if l[0] > -1 and l[1]>-1 and l[2]>-1:
if m == 0:
m = max(l)-min(l)+1
else:
m = min(m, max(l)-min(l)+1)
print(m)
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define bback(x) x.rbegin(),x.rend()
const int mod = 1e9+7;
int gcd(int a,int b){if(a<b)return gcd(b,a);else if(b==0)return a;else return gcd(b,a%b);}
int isPrime(int n){int p=(int)sqrt(n);for(int i=2;i<=p;i++) if(n%i==0) return 0;return 1;}// reuturn 1 if prime
int ceil(long double a){int b=a;if(a==b){return b;}else{return b+1;}}
int floor(long double a){int b=a;return b;}
int lcm(int a,int b){ return (a*b)/gcd(a,b);}
bool chk_balanced_parenthesis(string exp)
{
bool flag = true;
int count = 0;int n = exp.length();
for (int i = 0; i < n; i++) {
if (exp[i] == '(') {count++;}
else {count--;}
if (count < 0) {flag = false;break;}
}
if (count != 0) {flag = false;}
return flag;
}
bool IsPalindrome(const string& str){
if (str.empty())
return false;
int i = 0, j = str.length() - 1;
while (i < j){
if (str[i] != str[j]){
return false;
}
i++; j--;
}
return true;
}
string addBinary(string a, string b) {
string ans="";
int i=a.length()-1,j=b.length()-1,carry=0;
while(i>=0||j>=0||carry){
if(i>=0){carry+=a[i]-'0'; i--; }
if(j>=0){carry+=b[j]-'0'; j--; }
// cout<<carry<<" ";
ans+=(carry%2+'0');
cout<<ans<<" ";
// carry=carry/2;
// cout<<carry<<endl;
}
reverse(ans.begin(),ans.end());
return ans;
}
int array_gcd(vector<int>& nums) {
int mn = nums[0], mx = nums[0];
for(auto n: nums){if(n > mx) mx = n;if(n < mn) mn = n;}
for(int i = mn; i >= 1; i--){if((mn % i == 0) && (mx % i == 0)) return i;}
return 1;
}
int and_range(int m, int n) {
int i = 0;
while(m != n){
m >>= 1; n >>= 1; i++;
}
return m << i;
}
/********************************************************************************************************************************************/
void solve(){
string a;
cin >> a;
int n=a.size(),ans = INT_MAX;
// bool ans = false;
for(int i=1;i<a.size()-1;i++){
int j=i;
while(a[j+1]==a[i]&&j+1<n){
j++;
}
if(j+1<n&&i<n&&a[i-1]!=a[i]&&a[i]!=a[j+1]&&a[j+1]!=a[i-1]){
ans=min(ans,j-i+3);
}
i=j;
}
if(ans==INT_MAX){
cout << 0 << endl;
}
else{
cout << ans << endl;
}
}
/********************************************************************************************************************************************/
signed main(){
ios_base::sync_with_stdio(!cin.tie(nullptr));
int t;
cin>>t;
while(t--){
solve();
}
}
// int mn=*min_element(a.begin(), a.end());
// vector<int>a(n);
// for(int i=0;i<n;i++){
// cin>>a[i];
// }
// map<int,int>mp;
// for(int i=0;i<n;i++){
// int x;
// cin>>x;
// mp[x]++;
// }
// if(ans){
// cout<<"NO"<<endl;
// }
// else{
// cout<<"YES"<<endl;
// }
1647A - Madoka and Math Dad | 710A - King Moves |
1131A - Sea Battle | 118A - String Task |
236A - Boy or Girl | 271A - Beautiful Year |
520B - Two Buttons | 231A - Team |
479C - Exams | 1030A - In Search of an Easy Problem |
158A - Next Round | 71A - Way Too Long Words |
160A - Twins | 1A - Theatre Square |
1614B - Divan and a New Project | 791A - Bear and Big Brother |
1452A - Robot Program | 344A - Magnets |
96A - Football | 702B - Powers of Two |
1036A - Function Height | 443A - Anton and Letters |
1478B - Nezzar and Lucky Number | 228A - Is your horseshoe on the other hoof |
122A - Lucky Division | 1611C - Polycarp Recovers the Permutation |
432A - Choosing Teams | 758A - Holiday Of Equality |
1650C - Weight of the System of Nested Segments | 1097A - Gennady and a Card Game |