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

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

vector<int> countDiff(const string& S, const string& target){
	const int n = S.size();
	const int m = target.size();
	vector<int> res(n, INF);
	for(int i=0;i<=n-m;i++){
		int diff = 0;
		for(int j=0;j<m;j++){
			if(S[i+j] != target[j]){
				++diff;
			}
		}
		res[i] = diff;
	}
	return res;
}

int solve(const string& S){
	const int n = S.size();
	const auto goodDiff = countDiff(S, good);
	const auto problemDiff = countDiff(S, problem);

	auto afterProblem = problemDiff;
	for(int i=n-2;i>=0;i--){
		afterProblem[i] = min(afterProblem[i+1], afterProblem[i]);
	}

	vector<int> mustDelete(n, 0);
	for(int i=0;i<n;i++){
		if(goodDiff[i] == 0){
			++mustDelete[i+good.size()-1];
		}
		if(problemDiff[i] == 0){
			++mustDelete[i+problem.size()-1];
		}
	}
	for(int i=1;i<n;i++){
		mustDelete[i] += mustDelete[i-1];
	}

	int res = INF;
	for(int i=0;i<n;i++){
		if(i + good.size() + problem.size() > n){
			break;
		}
		int cost = goodDiff[i];
		if(i - 1 >= 0){
			cost += mustDelete[i-1];
		}
		cost += afterProblem[i+good.size()];
		res = min(res, cost);
	}
	return res;
}

int main(){
	int T;
	cin >> T;
    
	while(T--){
		string S;
		cin >> S;
		cout << solve(S) << endl;	
	}
    
	return 0;
}