#include using namespace std; const int MAXN = 1e6 + 6; const int INF = 0x3f3f3f3f; int n; string s; const string GOOD = "good"; const string PROB = "problem"; int gv[ MAXN ], pv[ MAXN ]; int pgv[ MAXN ], spv[ MAXN ]; void solve(){ for(int i = 3; i < n; ++i){ gv[ i ] = 4; for(int j = 0; j < 4; ++j) if( s[ i - 3 + j ] == GOOD[ j ] ) --gv[ i ]; } for(int i = 0; i + 6 < n; ++i){ pv[ i ] = 7; for(int j = 0; j < 7; ++j) if( s[ i + j ] == PROB[ j ] ) --pv[ i ]; } // memset( pgv, INF, sizeof(pgv) ); pgv[ 2 ] = INF; for(int i = 3; i < n; ++i) pgv[ i ] = min( pgv[ i - 1 ], gv[ i ] ); // memset( spv, INF, sizeof(spv) ); assert( n - 6 >= 0 ); spv[ n - 6 ] = INF; for(int i = n - 7; i >= 0; --i) spv[ i ] = min( spv[ i + 1 ], pv[ i ] ); int ans = INF; for(int i = 3; i + 6 < n; ++i) ans = min( ans, pgv[ i ] + spv[ i + 1 ] ); cout << ans << endl; } int main(){ ios::sync_with_stdio( false ); int T; cin >> T; while( T-- ){ cin >> s; n = s.size(); solve(); } return 0; }