import sys input = sys.stdin.readline M = int(input()) for _ in range(M): S = list(input()) N = len(S) T = [0] * 8 problem = "problem" for i in range(4, N - 7): cnt = 0 for j in range(7): cnt += int(S[i + j] != problem[j]) T[cnt] += 1 ans = 10 ** 18 good = "good" problem_cnt = 0 for i in range(N - 11): cnt = problem_cnt for j in range(4): cnt += int(S[i + j] != good[j]) temp = 10 ** 18 for j in range(8): if T[j]: temp = j break ans = min(ans, cnt + temp) cnt = 0 for j in range(7): if i + 4 + j >= N: break cnt += int(S[i + 4 + j] != problem[j]) T[cnt] -= 1 if i >= 6: if S[i-6:i+1] == list(problem): problem_cnt += 1 print(ans)