def solve(S, string1, string2): n = len(S) len1 = len(string1) len2 = len(string2) dp = [0] * n mindist = len1 for tail in range(len1, n-len2 + 1): dist = len([1 for c1, c2 in zip(string1, S[tail - len1:tail]) if c1 != c2]) if dist < mindist: mindist = dist dp[tail] = mindist if not mindist: break mindist = len2 for head in range(n-len2, len1 - 1, -1): dist = len([1 for c1, c2 in zip(string2, S[head:head + len2]) if c1 != c2]) if dist < mindist: mindist = dist dp[head] += mindist if not mindist: break count = 0 for i in range(len1 + len2, n - len2 + 1): if string2 == S[i-len1-len2:i-len1]: count += 1 dp[i] += count return min(dp[len1 : n - len2 + 1]) T = int(raw_input()) for t in range(T): print(solve(raw_input(), 'good', 'problem'))