#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int t; cin>>t; while(t--){
string s; cin>>s;
int n=s.size(),ans=2e6+6,a[4]={};
for(int r=0,l=0;r<n;r++){
a[s[r]-'0']++;
while(a[1]&&a[2]&&a[3]){
ans=min(ans,r-l+1);
a[s[l]-'0']--;
l++;
}
}
if(ans==2e6+6) ans=0;
cout<<ans<<'\n';
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBpbnQgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnNpZ25lZCBtYWluKCl7Cglpb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUobnVsbHB0cik7CglpbnQgdDsgY2luPj50OyB3aGlsZSh0LS0pewoJCXN0cmluZyBzOyBjaW4+PnM7IAoJCWludCBuPXMuc2l6ZSgpLGFucz0yZTYrNixhWzRdPXt9OwoJCWZvcihpbnQgcj0wLGw9MDtyPG47cisrKXsKCQkJYVtzW3JdLScwJ10rKzsKCQkJd2hpbGUoYVsxXSYmYVsyXSYmYVszXSl7CgkJCQlhbnM9bWluKGFucyxyLWwrMSk7CgkJCQlhW3NbbF0tJzAnXS0tOwoJCQkJbCsrOwoJCQl9CgkJfQoJCWlmKGFucz09MmU2KzYpIGFucz0wOwoJCWNvdXQ8PGFuczw8J1xuJzsKCX0KfQo=