// Arup Guha
// 4/15/2023
// Solution to 2023 Code Jam Farewell Round A Problem E: Untie

import java.util.*;

public class e {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		
		// Process cases.
		for (int loop=1; loop<=nC; loop++) {
			
			char[] s = stdin.next().toCharArray();
			
			// Remap to R=0,P=1,S=2
			int n = s.length;
			int[] seq = new int[n];
			for (int i=0; i<n; i++) {
				if (s[i] == 'R') seq[i] = 0;
				else if (s[i] == 'P') seq[i] = 1;
				else if (s[i] == 'S') seq[i] = 2;
			}
			
			int globalres = 10000000;
			
			// idea - set s[0] hard coded to R, P, S.
			for (int z=0; z<3; z++) {
				
				// This is the cost to changing index 0 to z.
				int add = (seq[0] == z) ? 0 : 1;				
				
				// Here is how we "force" the change.
				int[][] dp = new int[n][3];
				for (int j=0; j<3; j++) dp[0][j] = 10000000;
				dp[0][z] = add;
				
				// Run the DP.
				for (int i=1; i<n; i++) {
					for (int j=0; j<3; j++) {
						
						// Build off the best one that isn't the last letter.
						int min = 10000000;
						for (int k=0; k<3; k++) {
							if (k == j) continue;
							min = Math.min(min, dp[i-1][k]);
						}
						
						// This is what we add to change this letter to j.
						int add2 = seq[i] == j ? 0 : 1;
						
						// Our best cost.
						dp[i][j] = min + add2;
					}
				}
				
				// Our answer is anything but ending in character z...
				int res = dp[n-1][(z+1)%3];
				if (dp[n-1][(z+2)%3]+add < res) res = dp[n-1][(z+2)%3];
				
				// Update our best answer, if necessary.
				if (res < globalres) globalres = res;				
			}
			
			// Ta da!
			System.out.println("Case #"+loop+": "+globalres);
		}
	}
}