#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

const string good = "good";
const string problem = "problem";
int ns, ng, np;
string str;

int check_good(int pos) {
	int count = 0;
	for (int i = 0; i < ng; i++) {
		if (str[pos + i] != good[i]) {
			count++;
		}
	}
	return count;
}

int check_problem(int pos) {
	int count = 0;
	for (int i = 0; i < np; i++) {
		if (str[pos + i] != problem[i]) {
			count++;
		}
	}
	return count;
}

int main() {
	int t;

	cin >> t;
	vector<string> s(t, "");
	vector<int> ans(t, 0);
	for (int i = 0; i < t; i++) {
		cin >> s[i];
	}

	ng = good.size();
	np = problem.size();
	vector<int> count_g(101, 1e9);
	vector<int> count_p(101, 1e9);

	for (int i = 0; i < t; i++) {
		fill(count_g.begin(), count_g.end(), 1e9);
		fill(count_p.begin(), count_p.end(), 1e9);
		str = s[i];
		ns = str.size();
		for (int j = 0; j <= ns - ng; j++) {
			count_g[j] = check_good(j);
		}
		for (int j = ng; j <= ns - np; j++) {
			count_p[j] = check_problem(j);
		}

		int tmp = 1e9;
		for (int k = 0; k <= ns; k++) {
			for (int l = k + ng; l <= ns; l++) {
				tmp = min(tmp, count_g[k] + count_p[l]);
			}
		}
		ans[i] = tmp;
	}

	for (int i = 0; i < t; i++) {
		cout << ans[i] << endl;
	}

	return 0;
}