#include <bits/stdc++.h>
using namespace std;

const int INF = 1 << 25;
const string good = "good";
const string problem = "problem";

int main(){
    int T; cin >> T;
    
    while(T--){
        string S; cin >> S;
        
        int N = (int)S.size();
        vector<int> G(N, INF), P(N, INF);
        
        for(int i=0;i<N;i++){
            int t = i;
            int s = t - (int)good.size() + 1;
            if(s < 0)
                continue;
            
            int dif = 0;
            for(int j=0;j<good.size();j++){
                dif += S[s+j] != good[j];
            }
            
            G[i] = min(G[i-1], dif);
        }
        
        for(int i=N-1;i>=0;i--){
            int s = i;
            int t = s + (int)problem.size() - 1;
            if(t >= N)
                continue;
            
            int dif = 0;
            for(int j=0;j<problem.size();j++){
                dif += S[s+j] != problem[j];
            }
            
            P[i] = min(P[i+1], dif);
        }
        
        int res = INF;
        for(int i=1;i<N;i++)
            res = min(res, G[i-1] + P[i]);
        
        cout << res << endl;
    }
    
    return 0;
}