T = int(input()) S = [input() for i in range(T)] INF = 10**9+7 good = 'good' good_len = len(good) problem = 'problem' problem_len = len(problem) for t in range(T): N = len(S[t]) ans = INF imos = [INF for i in range(N+1)] for i in range(good_len,N-problem_len+1): cnt = 0 for k in range(problem_len): if S[t][i+k] != problem[k]: cnt+=1 imos[i] = cnt for i in range(N,0,-1): imos[i-1] = min(imos[i-1],imos[i]) #print(imos) for i in range(0,N-good_len-problem_len+1): good_cnt = 0 for j in range(good_len): if S[t][i+j] != good[j]: good_cnt+=1 ans = min(ans,good_cnt + imos[i+good_len]) print(ans)