#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; static const int MAX_LEN = 1000001; string nstr(){ static char res_[MAX_LEN]; scanf("%s",res_); return string(res_); } const int INF = INT_MAX; const string G = "good", P = "problem"; int dp[MAX_LEN][5][8]; void solve(string &S){ const int N = sz(S); rep(i, N + 1)rep(j, 5)rep(k, 8)dp[i][j][k] = INF; dp[0][0][0] = 0; rep(i, N)rep(j, 5)rep(k, 8)if(dp[i][j][k] != INF){ if(j != 4 && k == 7)continue; int x = dp[i][j][k]; // good if(j < 4)smin(dp[i + 1][j + 1][0], x + (S[i] != G[j])); // problem if(k < 7)smin(dp[i + 1][j == 4 ? 4 : 0][k + 1], x + (S[i] != P[k])); // good & problem // not smin(dp[i + 1][j == 4 ? 4 : 0][k == 7 ? 7 : 0], x + ((j < 4 && S[i] == G[j]) || (k < 7 && S[i] == P[k]))); // comp if(j == 4 && k == 7)smin(dp[i + 1][j][k], x); } cout << dp[N][4][7] << endl; } int main(){ int T; cin >> T; while(T--){ string S = nstr(); solve(S); } }