#include <stdio.h>
#include <string.h>

#define MAXV 10000
#define MAXR 101000
#define MAXC 5

char op[10], rem[MAXR];
int col[MAXC][MAXV+1];
int data[MAXR][MAXC];
int T,a,b,x,m,n;

int main(){
	scanf("%d",&T);
	for (int TC=1; TC<=T; TC++){
		printf("Case #%d:\n",TC);
		scanf("%d %d %d %d %d",&a,&b,&x,&m,&n);
		memset(data,0,sizeof(data));
		memset(rem,0,sizeof(rem));
		memset(col,0,sizeof(col));
		for (int r=0; r<n; r++)
			for (int c=0; c<MAXC; c++){
				long long v1 = (long long) a * (r==0?0:data[r-1][c]);
				long long v2 = (long long) b * (c==0?0:data[r][c-1]);
				data[r][c] = (v1 + v2 + x) % m;
				col[c][data[r][c]]++;
			}

		int q,r,c;
		scanf("%d",&q);
		while (q-- > 0){
			scanf("%s",op);
			if (op[0]=='i'){
				for (c=0; c<MAXC; c++){
					scanf("%d",&data[n][c]);
					col[c][data[n][c]]++;
				}
				n++;
			} else if (op[1]=='e'){
				scanf("%d",&r); r--;
				if (r<0 || r>=n || rem[r]) continue;
				for (c=0; c<MAXC; c++) col[c][data[r][c]]--;
				rem[r] = 1;
			} else if (op[2]=='x'){
				scanf("%d",&c); c--;
				for (int v=MAXV; v>=0; v--) if (col[c][v]){ printf("%d\n",v); break; }
			} else if (op[1]=='i'){
				scanf("%d",&c); c--;
				for (int v=0; v<=MAXV; v++) if (col[c][v]){ printf("%d\n",v); break; }
			} else {
				scanf("%d %d %d",&c,&a,&b); c--;
				int cnt = 0;
				for (int v=a; v<=b; v++) cnt += col[c][v];
				printf("%d\n",cnt);
			}
		}
	}
}
