def min_operations(s): target_good = 'good' target_problem = 'problem' len_s = len(s) min_k = float('inf') # Iterate over all possible starting indices for 'good' for i in range(len_s - 3): # Calculate changes needed to make 'good' starting at i good_changes = sum(1 for k in range(4) if s[i + k] != target_good[k]) # The earliest j starts at i+4, latest j can be len_s -7 j_start = i + 4 j_end = len_s - 7 if j_start > j_end: continue # Iterate over possible j values for j in range(j_start, j_end + 1): # Calculate changes needed to make 'problem' starting at j problem_changes = sum(1 for k in range(7) if s[j + k] != target_problem[k]) total = good_changes + problem_changes if total < min_k: min_k = total return min_k # Read input and output results T = int(input()) for _ in range(T): S = input().strip() print(min_operations(S))