#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import itertools # %% T = int(readline()) words = read().decode().split() # %% def solve(S): INF = 10 ** 7 L = len(S) problem = [i + 7 for i in range(L - 6) if S[i: i + 7] == 'problem'] G = [sum(x != y for x, y in zip(S[i: i + 4], 'good')) for i in range(L)] add = [0] * L for i in problem: if i < L: add[i] += 1 for n in range(L - 1): add[n + 1] += add[n] G = [x + y for x, y in zip(G, add)] G = list(itertools.accumulate([INF] * 4 + G[:-4], min)) for i in range(L - 6): G[i] += sum(x != y for x, y in zip(S[i: i + 7], 'problem')) return min(G[: L - 6]) for w in words: print(solve(w)) # %%