#include #define rep(i, a) for (int i = 0; i < (a); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define repr(i, a) for (int i = (a) - 1; i >= 0; i--) #define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--) #define itall(a) a.begin(), a.end() using namespace std; typedef long long ll; const ll inf = 1e9; const ll mod = 1e9 + 7; const double pi = acos(-1); const double eps = 1e-8; template inline T sq(T a) { return a * a; } template inline T cb(T a) { return a * a * a; } template inline bool umin(T &a, const T &b) { return b < a && (a = b, true); } template inline bool umax(T &a, const T &b) { return a < b && (a = b, true); } const int infP = inf; string a = "good", b = "problem"; int sum[1000010]; int hamA[1000010], hamB[1000010]; int hamBmin[1000010]; int main() { int q; cin >> q; rep (z, q) { string s; cin >> s; fill(sum, sum + s.length() + 10, 0); fill(hamA, hamA + s.length(), 0); fill(hamB, hamB + s.length(), 0); fill(hamBmin, hamBmin + s.length(), inf); for (int i = s.find(b); i != string::npos; i = s.find(b, i + 1)) { sum[i + b.length()]++; } rep2 (i, 1, s.length()) { sum[i] += sum[i - 1]; } rep (i, s.length() - 3) { rep (k, 4) hamA[i] += s[i + k] != a[k]; } rep (i, s.length() - 6) { rep (k, 7) hamB[i] += s[i + k] != b[k]; } hamBmin[s.length() - 7] = hamB[s.length() - 7]; repr (i, s.length() - 7) { umin(hamBmin[i], min(hamB[i], hamBmin[i + 1])); } int ans = inf; rep (i, s.length() - 10) { umin(ans, hamA[i] + sum[i] + hamBmin[i + 4]); } cout << ans << endl; } }