#include <bits/stdc++.h>
using namespace std;
const int N = 1234567;
char a[N];
int sum[10 * N];
int flag[10 * N];
char ss[55];
const int data[] = {3, 2, 1, 0};
inline int inv(int x) {
return data[x];
}
void build(int x, int l, int r) {
flag[x] = 0;
if (l == r) {
sum[x] = a[l] - '0';
} else {
int mid = (l + r) / 2;
build(x + x, l, mid);
build(x + x + 1, mid + 1, r);
sum[x] = sum[x + x] + sum[x + x + 1];
}
}
void push(int x, int l, int r) {
if (flag[x] == 1) {
sum[x] = 0;
flag[x + x] = flag[x + x + 1] = 1;
flag[x] = 0;
return;
}
if (flag[x] == 2) {
sum[x] = r - l + 1;
flag[x + x] = flag[x + x + 1] = 2;
flag[x] = 0;
return;
}
if (flag[x] == 3) {
sum[x] = r - l + 1 - sum[x];
flag[x + x] = inv(flag[x + x]);
flag[x + x + 1] = inv(flag[x + x + 1]);
flag[x] = 0;
return;
}
}
void assign(int x, int l, int r, int ll, int rr, int v) {
int mid = (l + r) / 2;
push(x, l, r);
push(x + x, l, mid);
push(x + x + 1, mid + 1, r);
if (l == ll && r == rr) {
sum[x] = v * (r - l + 1);
flag[x + x] = flag[x + x + 1] = v + 1;
} else {
int mid = (l + r) / 2;
if (rr <= mid) {
assign(x + x, l, mid, ll, rr, v);
} else if (ll > mid) {
assign(x + x + 1, mid + 1, r, ll, rr, v);
} else {
assign(x + x, l, mid, ll, mid, v);
assign(x + x + 1, mid + 1, r, mid + 1, rr, v);
}
sum[x] = sum[x + x] + sum[x + x + 1];
}
}
void inverse(int x, int l, int r, int ll, int rr) {
int mid = (l + r) / 2;
push(x, l, r);
push(x + x, l, mid);
push(x + x + 1, mid + 1, r);
if (l == ll && r == rr) {
sum[x] = r - l + 1 - sum[x];
flag[x + x] = inv(flag[x + x]);
flag[x + x + 1] = inv(flag[x + x + 1]);
} else {
int mid = (l + r) / 2;
if (rr <= mid) {
inverse(x + x, l, mid, ll, rr);
} else if (ll > mid) {
inverse(x + x + 1, mid + 1, r, ll, rr);
} else {
inverse(x + x, l, mid, ll, mid);
inverse(x + x + 1, mid + 1, r, mid + 1, rr);
}
sum[x] = sum[x + x] + sum[x + x + 1];
}
}
int query(int x, int l, int r, int ll, int rr) {
int mid = (l + r) / 2;
push(x, l, r);
push(x + x, l, mid);
push(x + x + 1, mid + 1, r);
if (l == ll && r == rr) {
return sum[x];
} else {
int mid = (l + r) / 2;
if (rr <= mid) {
return query(x + x, l, mid, ll, rr);
}
if (ll > mid) {
return query(x + x + 1, mid + 1, r, ll, rr);
}
return query(x + x, l, mid, ll, mid) + query(x + x + 1, mid + 1, r, mid + 1, rr);
}
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int tests;
scanf("%d", &tests);
int tcase = 0;
while (tests--) {
tcase++;
printf("Case %d:\n", tcase);
int m;
scanf("%d", &m);
int s = 0;
while (m--) {
int t;
scanf("%d%s", &t, ss + 1);
int n = strlen(ss + 1);
for (int i = 0; i < t; i++) {
for (int j = s + 1; j <= s + n; j++) {
a[j] = ss[j - s];
}
s += n;
}
}
build(1, 1, s);
int q;
scanf("%d", &q);
int qry = 0;
while (q--) {
char ch;
int x, y;
scanf(" %c%d%d", &ch, &x, &y);
x++; y++;
if (ch == 'F') {
assign(1, 1, s, x, y, 1);
}
if (ch == 'E') {
assign(1, 1, s, x, y, 0);
}
if (ch == 'I') {
inverse(1, 1, s, x, y);
}
if (ch == 'S') {
qry++;
printf("Q%d: %d\n", qry, query(1, 1, s, x, y));
}
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE4gPSAxMjM0NTY3OwoKY2hhciBhW05dOwppbnQgc3VtWzEwICogTl07CmludCBmbGFnWzEwICogTl07CmNoYXIgc3NbNTVdOwoKY29uc3QgaW50IGRhdGFbXSA9IHszLCAyLCAxLCAwfTsKCmlubGluZSBpbnQgaW52KGludCB4KSB7CiAgcmV0dXJuIGRhdGFbeF07Cn0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cy0tKSB7CiAgICB0Y2FzZSsrOwogICAgcHJpbnRmKCJDYXNlICVkOlxuIiwgdGNhc2UpOwogICAgaW50IG07CiAgICBzY2FuZigiJWQiLCAmbSk7CiAgICBpbnQgcyA9IDA7CiAgICB3aGlsZSAobS0tKSB7CiAgICAgIGludCB0OwogICAgICBzY2FuZigiJWQlcyIsICZ0LCBzcyArIDEpOwogICAgICBpbnQgbiA9IHN0cmxlbihzcyArIDEpOwogICAgICBmb3IgKGludCBpID0gMDsgaSA8IHQ7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSBzICsgMTsgaiA8PSBzICsgbjsgaisrKSB7CiAgICAgICAgICBhW2pdID0gc3NbaiAtIHNdOwogICAgICAgIH0KICAgICAgICBzICs9IG47CiAgICAgIH0KICAgIH0KICAgIGJ1aWxkKDEsIDEsIHMpOwogICAgaW50IHE7CiAgICBzY2FuZigiJWQiLCAmcSk7CiAgICBpbnQgcXJ5ID0gMDsKICAgIHdoaWxlIChxLS0pIHsKICAgICAgY2hhciBjaDsKICAgICAgaW50IHgsIHk7CiAgICAgIHNjYW5mKCIgJWMlZCVkIiwgJmNoLCAmeCwgJnkpOwogICAgICB4Kys7IHkrKzsKICAgICAgaWYgKGNoID09ICdGJykgewogICAgICAgIGFzc2lnbigxLCAxLCBzLCB4LCB5LCAxKTsKICAgICAgfSAKICAgICAgaWYgKGNoID09ICdFJykgewogICAgICAgIGFzc2lnbigxLCAxLCBzLCB4LCB5LCAwKTsKICAgICAgfQogICAgICBpZiAoY2ggPT0gJ0knKSB7CiAgICAgICAgaW52ZXJzZSgxLCAxLCBzLCB4LCB5KTsKICAgICAgfQogICAgICBpZiAoY2ggPT0gJ1MnKSB7CiAgICAgICAgcXJ5Kys7CiAgICAgICAgcHJpbnRmKCJRJWQ6ICVkXG4iLCBxcnksIHF1ZXJ5KDEsIDEsIHMsIHgsIHkpKTsKICAgICAgfQogICAgfQogIH0KICByZXR1cm4gMDsKfQ==