#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define MAX 200005
void solve(){
vector<int> L1(MAX),L2(MAX),L3(MAX),R1(MAX),R2(MAX),R3(MAX);
string s;
cin>>s;
int n=s.size();
for(int i=0;i<n;i++){
if(s[i]=='1'){
L1[i+1]=i+1;
L2[i+1]=L2[i];
L3[i+1]=L3[i];
}
else if(s[i] == '2'){
L2[i+1]=i+1;
L1[i+1]=L1[i];
L3[i+1]=L3[i];
}
else{
L3[i+1]=i+1;
L1[i+1]=L1[i];
L2[i+1]=L2[i];
}
}
for(int i=n-1;i>=0;i--){
if(s[i]=='1'){
R1[i+1]=i+1;
R2[i+1]=R2[i+2];
R3[i+1]=R3[i+2];
}
else if(s[i] == '2'){
R2[i+1]=i+1;
R1[i+1]=R1[i+2];
R3[i+1]=R3[i+2];
}
else{
R3[i+1]=i+1;
R1[i+1]=R1[i+2];
R2[i+1]=R2[i+2];
}
}
int cur=1e8;
for(int i=0;i<n;i++){
if(s[i]=='1' && R2[i+2]!=0 && R3[i+2]!=0){
//cout<<max(R2[i+2],R3[i+2]) - L1[i+1] + 1<<endl;
cur = min(cur,max(R2[i+2],R3[i+2]) - L1[i+1] + 1);
}
if(s[i]=='2' && R1[i+2]!=0 && R3[i+2]!=0){
//cout<<max(R1[i+2],R2[i+2]) - L3[i+1] + 1<<endl;
cur = min(cur,max(R3[i+2],R1[i+2]) - L2[i+1] + 1);
}
if(s[i]=='3' && R2[i+2]!=0 && R1[i+2]!=0){
//cout<<max(R1[i+2],R2[i+2]) - L3[i+1] + 1<<endl;
cur=min(cur,max(R1[i+2],R2[i+2]) - L3[i+1] + 1);
}
}
if(cur == (int) 1e8){
cout<<0<<endl;
return;
}
cout<<cur<<endl;
}
//#undef int
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
//#define int long long
int t;
cin>>t;
while(t--){
solve();
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}