#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 200010;
const int M = 450;
struct Block
{
int l, r;
LL lazy1, lazy2, lazy3, sum;
LL c1, c2;
inline int len()
{
return r - l + 1;
}
};
Block B[M];
LL arr[N];
int belong[N], unit, bcnt;
int n, m;
void init()
{
int i;
unit = sqrt(n);
bcnt = n / unit;
if (n % unit)
++bcnt;
for (i = 1; i <= bcnt; ++i)
{
B[i].l = (i - 1) * unit + 1;
B[i].r = i * unit;
B[i].sum = 0LL;
B[i].lazy1 = 0LL;
B[i].lazy2 = 0LL;
B[i].lazy3 = -INF;
B[i].c1 = B[i].c2 = 0LL;
}
B[bcnt].r = n;
for (i = 1; i <= n; ++i)
{
arr[i] = 0LL;
belong[i] = (i - 1) / unit + 1;
}
}
void pushdown(int bx)
{
if (B[bx].lazy3 != -INF)
{
for (int i = B[bx].l; i <= B[bx].r; ++i)
arr[i] = B[bx].lazy3;
B[bx].lazy3 = -INF;
}
if (B[bx].lazy1)
{
for (int i = B[bx].l; i <= B[bx].r; ++i)
{
arr[i] += + B[bx].lazy1;
B[bx].lazy1 += B[bx].c1;
}
B[bx].lazy1 = 0LL;
B[bx].c1 = 0LL;
}
if (B[bx].lazy2)
{
for (int i = B[bx].r; i >= B[bx].l; --i)
{
arr[i] += + B[bx].lazy2;
B[bx].lazy2 += B[bx].c2;
}
B[bx].lazy2 = 0LL;
B[bx].c2 = 0LL;
}
B[bx].sum = 0LL;
for (int i = B[bx].l; i <= B[bx].r; ++i)
B[bx].sum += arr[i];
}
void update(int l, int r, int ops, LL val = 0LL)
{
int bl = belong[l], br = belong[r];
int i;
if (bl == br)
{
pushdown(bl);
if (ops == 1)
{
for (i = l; i <= r; ++i)
{
B[bl].sum -= arr[i];
arr[i] = arr[i] + (LL)(i - l + 1LL);
B[bl].sum += arr[i];
}
}
else if (ops == 2)
{
for (i = r; i >= l; --i)
{
B[bl].sum -= arr[i];
arr[i] = arr[i] + (LL)(r - i + 1LL);
B[bl].sum += arr[i];
}
}
else if (ops == 3)
{
for (i = l; i <= r; ++i)
{
B[bl].sum -= arr[i];
arr[i] = val;
B[bl].sum += arr[i];
}
}
}
else
{
//left
pushdown(bl);
if (ops == 1)
{
for (i = l; i <= B[bl].r; ++i)
{
B[bl].sum -= arr[i];
arr[i] = arr[i] + (LL)(i - l + 1LL);
B[bl].sum += arr[i];
}
}
else if (ops == 2)
{
for (i = B[bl].r; i >= l; --i)
{
B[bl].sum -= arr[i];
arr[i] = arr[i] + (LL)(r - i + 1LL);
B[bl].sum += arr[i];
}
}
else if (ops == 3)
{
for (i = l; i <= B[bl].r; ++i)
{
B[bl].sum -= arr[i];
arr[i] = val;
B[bl].sum += arr[i];
}
}
//right
pushdown(br);
if (ops == 1)
{
for (i = B[br].l; i <= r; ++i)
{
B[br].sum -= arr[i];
arr[i] = arr[i] + (LL)(i - l + 1LL);
B[br].sum += arr[i];
}
}
else if (ops == 2)
{
for (i = r; i >= B[br].l; --i)
{
B[br].sum -= arr[i];
arr[i] = arr[i] + (LL)(r - i + 1LL);
B[br].sum += arr[i];
}
}
else if (ops == 3)
{
for (i = B[br].l; i <= r; ++i)
{
B[br].sum -= arr[i];
arr[i] = val;
B[br].sum += arr[i];
}
}
for (i = bl + 1; i < br; ++i)
{
if (ops == 3)
{
B[i].lazy3 = val;
B[i].c1 = 0LL;
B[i].c2 = 0LL;
B[i].lazy1 = 0LL;
B[i].lazy2 = 0LL;
}
else if (ops == 2)
{
B[i].lazy2 += (LL)(r - B[i].r + 1LL);
++B[i].c2;
}
else if (ops == 1)
{
B[i].lazy1 += (LL)(B[i].l - l + 1LL);
++B[i].c1;
}
}
}
}
LL query(int l, int r)
{
int bl = belong[l], br = belong[r], i;
LL sum = 0LL;
if (bl == br)
{
pushdown(bl);
for (i = l; i <= r; ++i)
sum += arr[i];
return sum;
}
else
{
pushdown(bl);
pushdown(br);
for (i = l; i <= B[bl].r; ++i)
sum += arr[i];
for (i = B[br].l; i <= r; ++i)
sum += arr[i];
for (i = bl + 1; i < br; ++i)
{
if (B[i].lazy3 != -INF)
sum += (LL)B[i].len() * B[i].lazy3;
if (B[i].lazy1)
sum += (B[i].lazy1 + B[i].lazy1 + (LL)(B[i].len() - 1LL) * B[i].c1) * (LL)B[i].len() / 2LL;
if (B[i].lazy2)
sum += (B[i].lazy2 + B[i].lazy2 + (LL)(B[i].len() - 1LL) * B[i].c2) * (LL)B[i].len() / 2LL;
if (B[i].lazy3 == -INF)
sum += B[i].sum;
}
return sum;
}
}
int main(void)
{
// fin("test0.in");
// fout("data_wa.txt");
int tcase, l, r, v;
char ops[4];
scanf("%d", &tcase);
while (tcase--)
{
scanf("%d%d", &n, &m);
init();
while (m--)
{
scanf("%s", ops);
if (ops[0] == ‘A‘)
{
scanf("%d%d", &l, &r);
update(l, r, 1);
}
else if (ops[0] == ‘B‘)
{
scanf("%d%d", &l, &r);
update(l, r, 2);
}
else if (ops[0] == ‘C‘)
{
scanf("%d%d%d", &l, &r, &v);
update(l, r, 3, (LL)v);
}
else
{
scanf("%d%d", &l, &r);
printf("%lld\n", query(l, r));
}
}
}
return 0;
}