T = int(input()) for t in range(1, T+1): C = input().strip() n = len(C) changes = 0 for i in range(n): # check if current choice is the same as neighbors if C[i] == C[(i-1)%n] or C[i] == C[(i+1)%n]: changes += 1 # count occurrences of each choice count = {'R': 0, 'P': 0, 'S': 0} for choice in 'RPS': # count occurrences of choice among neighbors count[choice] += (C[(i-1)%n] == choice) + (C[(i+1)%n] == choice) # choose the least frequent choice least_frequent = min(count, key=count.get) # change current choice to least frequent choice C = C[:i] + least_frequent + C[i+1:] print(f"Case #{t}: {changes}")