from sys import stdin def solve(S): L = len(S) # Leading problems lp = [0] * L for i in xrange(L-6): flag = True lp[i+6] = lp[i+5] for j in xrange(7): if S[i+j] != "problem"[j]: flag = False if flag: lp[i+6] += 1 # good cost at i good = [4] * L for i in xrange(L-10): for j in xrange(4): if S[i+j] == "good"[j]: good[i] -= 1 # problem cost at i problem = [7] * L for i in xrange(L-6): for j in xrange(7): if S[i+j] == "problem"[j]: problem[i] -= 1 for i in xrange(L-6, -1, -1): problem[i] = min(problem[i+1], problem[i]) ans = good[0] + problem[4] for i in xrange(1, L-10): ans = min(ans, good[i] + problem[i+4] + lp[i-1]) print(ans) lines = stdin.read().split('\n') T = int(lines[0]) for i in xrange(T): solve(lines[i+1])