#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include<map>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <iomanip>
using namespace std;
#define MAX_MOD 1000000007
#define REP(i,n) for(int i = 0;i < n;++i)
string aaa[2] = { "good","problem" };
int main(){
	int t;
	cin >> t;
	for (int testcase = 0;testcase < t;++testcase) {
		int go_back[200] = {};
		int go_forward[200] = {};
		string hoge;
		cin >> hoge;
		for (int i = 0;i < hoge.length() - 3;++i) {
			int tmp = 0;
			for (int q = 0;q < 4;++q) {
				if (hoge[i + q] != aaa[0][q]) tmp++;
			}
			go_back[i + 4] = tmp;
		}
		for (int i = 4;i < hoge.length() - 6;++i) {
			int tmp = 0;
			for (int q = 0;q < 7;++q) {
				if (hoge[i + q] != aaa[1][q]) tmp++;
			}
			go_forward[i] = tmp;
		}
		int ans = 100;
		for (int q = 4;q < hoge.length() - 6;++q) {
			int tmp = 100;
			for (int j = q;j < hoge.length() - 6;++j) {
				tmp = min(tmp, go_forward[j]);
			}
			ans = min(ans, tmp + go_back[q]);
		}
		cout << ans << endl;
	}
}