def diff(s1, s2): c = 0 for i in range(len(s1)): if s1[i]!=s2[i]: c += 1 return c def solve(): diff_p = [0]*1000000 diff_g = [0]*1000000 diff_pp = [0]*1000000 MAX_COST = 100 s = input() cost = MAX_COST diff_pp[len(s)-6] = MAX_COST for i in range(len(s)-7, -1, -1): diff_p[i] = diff(s[i:i+7], "problem") diff_g[i] = diff(s[i:i+4], "good") diff_pp[i] = min(diff_p[i], diff_pp[i+1]) p_cost = 0 for i in range(len(s)-10): if i>=7: if diff_p[i-7] == 0: p_cost += 1 cost = min(cost, diff_g[i]+diff_pp[i+4]+p_cost) print(cost) def main(): l = int(input()) for i in range(l): solve() main()