#include <cstdio>
#include <cstring>
#include <cassert>
using namespace std;

typedef long long LL;

int B, P, L, N;
LL pow[100005];

const int MAXN = 100005;

struct SegTree {
	struct Node {
		int l, r;
		LL sum;
		int mm() {return (l+r)>>1;}
	} v[MAXN<<2];
	int ll(int x) {return x<<1;}
	int rr(int x) {return (x<<1)|1;}
	void build(int l, int r, int x=1) {
		v[x].l = l, v[x].r = r;
		v[x].sum = 0;
		if (l + 1 == r) return ;
		int m = (l + r) >> 1;
		build(l, m, ll(x));
		build(m, r, rr(x));
	}
	void update(int k, int val, int x=1) {
		assert(v[x].l <= k && k < v[x].r);
		if (v[x].l + 1 == v[x].r) {
			assert(v[x].l == k);
			v[x].sum = val % P;
			return ;
		}
		int m = v[x].mm();
		if (k < m) update(k, val, ll(x));
		else update(k, val, rr(x));
		v[x].sum = (v[ll(x)].sum * pow[v[x].r-m] % P + v[rr(x)].sum) % P;
	}
	LL query(int l, int r, int x=1) {
		if (l <= v[x].l && v[x].r <= r) {
			return v[x].sum % P;
		}
		LL ret = 0; int m = v[x].mm();
		if (r <= m) {
			ret = query(l, r, ll(x));
		} else if (l >= m) {
			ret = query(l, r, rr(x));
		} else {
			ret = (pow[r-m] * query(l, r, ll(x)) % P + query(l, r, rr(x))) % P;
		}
		return ret % P;
	}
};

class BITree {
public:
	static const int OFFSET = 10, SIZE = 2*OFFSET + MAXN;
	LL u[SIZE];
	void clear() {
		memset(u, 0, sizeof(u));
	}
	void modify(int x, LL v) {
		for (x+=OFFSET; x<SIZE; x+=x&-x) u[x] = (u[x] + v + P) % P;
	}
	LL getsum(int x) {
		LL s = 0;
		for (x+=OFFSET; x; x-=x&-x) s = (s + u[x]) % P;
		return s;
	}
} tr;

LL power(LL x, int n) {
	LL ret = 1;
	for (; n; n>>=1, x=x*x%P) {
		if (n & 1) ret = ret * x % P;
	}
	return ret;
}

LL a[MAXN];


int main() {
	while (4==scanf("%d%d%d%d",&B,&P,&L,&N) && B) {
		pow[0] = 1;
		for (int i = 1; i <= L; ++i) {
			pow[i] = pow[i-1] * B % P;
			a[i] = 0;
		}
		tr.clear();
//		tr.build(1, L+1);
		char op[2];
		for (int x,y,i = 0; i < N; ++i) {
			scanf("%s%d%d", op, &x, &y);
			if (op[0] == 'E') {
				assert(x >= 1 && x <= L);
				assert(y >= 0 && y < B);
				LL v = a[x] * pow[L-x] % P;
				tr.modify(x, -v);
				v = y * pow[L-x] % P;
				tr.modify(x, v);
				a[x] = y;
			} else {
				assert(x <= y && x >= 1 && y <= L);
				LL hx = tr.getsum(x-1);
				LL hy = tr.getsum(y);
				LL a = power(pow[L-y], P-2);
				LL ans = ((hy - hx + P) % P * a) % P;
				printf("%lld\n", ans);
			}
		}
		puts("-");
	}
}
