import java.util.*;

public class Query {
	public static void main(String[] args){
		final int MAXV = 10000, MAXR = 100100, MAXC = 5;
		boolean[] rem = new boolean[MAXR];
		int[][] col = new int[MAXC][MAXV+1];
		int[][] arr = new int[MAXR][MAXC];

		Scanner scan = new Scanner(System.in);
		int T = scan.nextInt();
		for (int TC=1; TC<=T; TC++){
			System.out.printf("Case #%d:\n",TC);

			int a = scan.nextInt();
			int b = scan.nextInt();
			int x = scan.nextInt();
			int m = scan.nextInt();
			int n = scan.nextInt();
			Arrays.fill(rem,false);
			for (int[] c : col) Arrays.fill(c,0);

			for (int r=0; r<n; r++)
				for (int c=0; c<MAXC; c++){
					long v1 = (long) a * (r==0?0:arr[r-1][c]);
					long v2 = (long) b * (c==0?0:arr[r][c-1]);
					arr[r][c] = (int) ((v1 + v2 + x) % m);
					col[c][arr[r][c]]++;
				}

			int q = scan.nextInt();
			while (q-- > 0){
				String op = scan.next();
				if (op.charAt(0)=='i'){
					for (int c=0; c<MAXC; c++){
						arr[n][c] = scan.nextInt();
						col[c][arr[n][c]]++;
					}
					n++;
				} else if (op.charAt(1)=='e'){
					int r = scan.nextInt() - 1;
					if (r<0 || r>=n || rem[r]) continue;
					for (int c=0; c<MAXC; c++) col[c][arr[r][c]]--;
					rem[r] = true;
				} else if (op.charAt(2)=='x'){
					int c = scan.nextInt() - 1;
					for (int v=MAXV; v>=0; v--) if (col[c][v]>0){
						System.out.printf("%d\n",v); break; }
				} else if (op.charAt(1)=='i'){
					int c = scan.nextInt() - 1;
					for (int v=0; v<=MAXV; v++) if (col[c][v]>0){
						System.out.printf("%d\n",v); break; }
				} else {
					int c = scan.nextInt() - 1;
					int L = scan.nextInt();
					int R = scan.nextInt();
					int cnt = 0;
					for (int v=L; v<=R; v++) cnt += col[c][v];
					System.out.printf("%d\n",cnt);
				}
			}
		}
	}
}
