#define _CRT_SECURE_NO_WARNINGS //#define _GLIBCXX_DEBUG #include using namespace std; typedef long long ll; typedef vector vi; typedef vector vvi; typedef pair pii; #define all(c) (c).begin(), (c).end() #define loop(i,a,b) for(ll i=a; i istream & operator>>(istream & is, vector &); template ostream & operator<<(ostream & os, vector const &); template typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple const &){} template typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple const & t){ os << (n==0?"":" ") << get(t); _ot(os, t); } template ostream & operator<<(ostream & os, tuple const & t){ _ot<0>(os, t); return os; } template typename enable_if<(n>=sizeof...(T))>::type _it(istream &, tuple &){} template typename enable_if<(n< sizeof...(T))>::type _it(istream & is, tuple & t){ is >> get(t); _it(is, t); } template istream & operator>>(istream & is, tuple & t){ _it<0>(is, t); return is; } template istream & operator<<(istream & is, pair & p){ return is >> p.first >> p.second; } template ostream & operator<<(ostream & os, pair const & p){ return os << "(" << p.first << ", " << p.second << ") "; } template istream & operator>>(istream & is, vector & v){ rep(i,v.size()) is >> v[i]; return is; } template ostream & operator<<(ostream & os, vector const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; } #ifdef DEBUG #define dump(...) (cerr<<#__VA_ARGS__<<" = "<> 1) + (x & 0x5555555555555555ULL); x = ((x & 0xccccccccccccccccULL) >> 2) + (x & 0x3333333333333333ULL); x = ((x & 0xf0f0f0f0f0f0f0f0ULL) >> 4) + (x & 0x0f0f0f0f0f0f0f0fULL); x = ((x & 0xff00ff00ff00ff00ULL) >> 8) + (x & 0x00ff00ff00ff00ffULL); x = ((x & 0xffff0000ffff0000ULL) >> 16) + (x & 0x0000ffff0000ffffULL); x = ((x & 0xffffffff00000000ULL) >> 32) + (x & 0x00000000ffffffffULL); return (int)x; } int find_good(string const & good, string const & s){ rep(i,s.size()-3){ bool ok = true; rep(j,4){ if(good[j]=='*') continue; if(s[i+j]!=good[j]){ ok = false; break; } } if(ok) return i; } return -1; } int find_prob(string const & prob, string const & s){ for(int i = s.size()-7; i>=0; i--){ bool ok = true; rep(j,7){ if(prob[j]=='*') continue; if(s[i+j]!=prob[j]){ //dump(s[i+j],prob[j]); ok = false; break; } } if(ok) return i; } return -1; } int main(){ // string s = "problemgood"; // cout << find_prob("problem",s) << endl; // cout << find_good("good",s) << endl; // return 0; int n; while(cin >> n){ rep(i,n){ int ans = 11; string s; cin >> s; rep(X,1<<4){ rep(Y,1<<7){ string x = good, y = prob; rep(j,4)if(X>>j&1) x[j] = '*'; rep(j,7)if(Y>>j&1) y[j] = '*'; int a = find_good(x,s); int b = find_prob(y,s); int p = popcount(X)+popcount(Y); if(a==-1 || b==-1) continue; if(a+4<=b){ if(ans > p){ ans = p; //dump(a,b); } } } } cout << ans << endl; } } }