//https://ncode.syosetu.com/n4830bu/252/ #include using namespace std; int main() { int T; cin >> T; while (T--) { string S; cin >> S; int N = S.size(); string good = "good", problem = "problem"; vector maine(N, 10); for (int i = N - 7; i >= 0; i--) { int cnt = 0; for (int j = 0; j < 7; j++) { if (S[i + j] != problem[j]) cnt++; } maine[i] = min(maine[i + 1], cnt); } vector found_problem(N); for (int i = 0; i <= N - 7; i++) { int cnt = 0; if (S.substr(i, 7) == problem) cnt = 1; found_problem[i] = (i == 0 ? cnt : cnt + found_problem[i - 1]); } int ans = 11; for (int i = N - 10; i >= 0; i--) { int cnt = 0; for (int j = 0; j < 4; j++) { if (S[i + j] != good[j]) cnt++; } ans = min(ans, cnt + maine[i + 4] + (i >= 7 ? found_problem[i - 7] : 0)); } cout << ans << endl; } }