public static void main(String args[]) throws Throwable{
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb=new StringBuilder();
		for(StringTokenizer st;(st=new StringTokenizer(in.readLine()))!=null;){
			long B=Long.parseLong(st.nextToken());
			P=Long.parseLong(st.nextToken());
			int L=parseInt(st.nextToken()),N=parseInt(st.nextToken());
			if(B==0&&P==0&&L==0&&N==0)break;
			long[] potMod=new long[L];
			long mod=BigInteger.valueOf(B).modInverse(BigInteger.valueOf(P)).longValue();
			potMod[L-1]=1;
			for(int i=L-2;i>=0;i--)potMod[i]=(potMod[i+1]*mod)%P;
			SegmentTree tree=new SegmentTree(new long[L],B);
			for(int i=0;i<N;i++) {
				st=new StringTokenizer(in.readLine());
				char p=st.nextToken().charAt(0);
				if(p=='E')
					tree.setValor(parseInt(st.nextToken())-1,Long.parseLong(st.nextToken()));
				else if(p=='H'){
					int a=parseInt(st.nextToken())-1,b=parseInt(st.nextToken())-1;
					long val=tree.getValor(a,b+1);
					val=((val%P)*(potMod[b]%P))%P;
					sb.append(val);
					sb.append("\n");
				}
			}
			sb.append("-\n");
		}
		System.out.print(new String(sb));
	}