#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL a[100];

int main()
{
	int T;
	scanf("%d", &T);
	for (int cas = 1; cas <= T; cas++) {
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++) {
			scanf("%lld", a + i);
		}
		int Q;
		scanf("%d", &Q);
		while (Q--) {
			char opt[10];
			int L, R;
			scanf("%s%d%d", opt, &L, &R);
			if (*opt == 'Q') {
				LL res = 1;
				for (int i = L; i <= R; i++) {
					res = res * a[i] % m;
				}
				printf("%lld\n", res);
			}
			if (*opt == 'M') {
				int X;
				scanf("%d", &X);
				for (int i = L; i <= R; i++) {
					a[i] = a[i] * X % m;
				}
			}
			if (*opt == 'D') {
				assert(0);
			}
		}
	}
	return 0;
}
