#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np # %% T = int(readline()) words = read().split() # %% def solve(S): S = np.array(list(S)) L = len(S) P = np.zeros(L - 6, np.int32) for i, ch in enumerate(b'problem'): P += 1 * (S[i: L - 6 + i] != ch) G = np.zeros(L - 4, np.int32) for i, ch in enumerate(b'good'): G += 1 * (S[i: L - 4 + i] != ch) add = np.zeros_like(G) add[7:] += (P == 0)[:-5] np.cumsum(add, out=add) G += add INF = 10 ** 7 return (np.minimum.accumulate(G[:L - 10]) + P[4:]).min() for w in words: print(solve(w)) # %%