//Misc;Segment Tree;Lazy Propagation
#include <iostream>
#include <string>
#include <cstring>
#define MAX 3000100
#define ull long long
using namespace std;
struct Node {
int a, b;
int pending;
Node() : a(0), b(0), pending(0) {}
Node(int a) : a(a), b(0), pending(0) { }
Node(int a, int b) : a(a), b(b), pending(0) { }
Node change(int n) {
if (n==1) {
b += a;
a = 0;
pending = n;
} else if (n==2) {
a += b;
b = 0;
pending = n;
} else if (n==3) {
swap(a, b);
pending = 3-pending;
}
return *this;
}
Node operator +(Node x) {
return Node(a+x.a, b+x.b);
}
};
struct Segtree {
Node T[MAX];
int n;
void clear(int n, int *P) {
this->n = n;
build(1, 1, n, P);
}
Node build(int v, int a, int b, int *P) {
if (a==b)
return T[v] = Node(1-P[a], P[a]);
else
return T[v] =
build(2*v, a, (a+b)/2, P) +
build(2*v+1, (a+b)/2+1, b, P);
}
Node update(int v, int a, int b, int i, int j, int carry, int increment) {
T[v].change(carry);
if (i>b || j<a)
return Node(0);
if (i<=a && b<=j)
return T[v].change(increment);
Node answer =
update(v*2, a, (a+b)/2, i, j, T[v].pending, increment) +
update(v*2+1, (a+b)/2+1, b, i, j, T[v].pending, increment);
T[v] = T[v*2] + T[v*2+1];
return answer;
}
Node update(int i, int j, int inc) {
return update(1, 1, n, i, j, 0, inc);
}
Node query(int i, int j) {
return update(i, j, 0);
}
};
Segtree T;
int P[MAX];
string s;
int main() {
int cases; cin >> cases;
for(int tt=1; tt<=cases; tt++) {
cout << "Case " << tt << ":" << endl;
int m; cin >> m;
int n = 0;
for(int i=0; i<m; i++) {
int t;
cin >> t >> s;
for(int j=0; j<t; j++) {
for(int k=0; k<s.size(); k++) {
P[++n] = s[k]-'0';
}
}
}
T.clear(n, P);
int q; cin >> q;
int query = 0;
while(q--) {
char cmd; int a, b;
cin >> cmd >> a >> b;
a++; b++;
if (cmd == 'F') {
T.update(a, b, 1);
} else if (cmd == 'E') {
T.update(a, b, 2);
} else if (cmd == 'I') {
T.update(a, b, 3);
} else {
Node node = T.query(a, b);
cout << "Q" << ++query << ": " << node.b << endl;
}
}
}
}
Ly9NaXNjO1NlZ21lbnQgVHJlZTtMYXp5IFByb3BhZ2F0aW9uCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNkZWZpbmUgTUFYIDMwMDAxMDAKI2RlZmluZSB1bGwgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgTm9kZSB7IAogICAgaW50IGEsIGI7CiAgICBpbnQgcGVuZGluZzsKCiAgICBOb2RlKCkgOiBhKDApLCBiKDApLCBwZW5kaW5nKDApIHt9CiAgICBOb2RlKGludCBhKSA6IGEoYSksIGIoMCksIHBlbmRpbmcoMCkgeyB9CiAgICBOb2RlKGludCBhLCBpbnQgYikgOiBhKGEpLCBiKGIpLCBwZW5kaW5nKDApIHsgfQoKICAgIE5vZGUgY2hhbmdlKGludCBuKSB7CiAgICAgICAgaWYgKG49PTEpIHsKICAgICAgICAgICAgYiArPSBhOwogICAgICAgICAgICBhID0gMDsKICAgICAgICAgICAgcGVuZGluZyA9IG47CiAgICAgICAgfSBlbHNlIGlmIChuPT0yKSB7IAogICAgICAgICAgICBhICs9IGI7CiAgICAgICAgICAgIGIgPSAwOwogICAgICAgICAgICBwZW5kaW5nID0gbjsKICAgICAgICB9IGVsc2UgaWYgKG49PTMpIHsKICAgICAgICAgICAgc3dhcChhLCBiKTsKICAgICAgICAgICAgcGVuZGluZyA9IDMtcGVuZGluZzsKICAgICAgICB9CgogICAgICAgIHJldHVybiAqdGhpczsKICAgIH0KICAgIAogICAgTm9kZSBvcGVyYXRvciArKE5vZGUgeCkgewogICAgICAgIHJldHVybiBOb2RlKGEreC5hLCBiK3guYik7ICAgIAogICAgfQp9OwoKc3RydWN0IFNlZ3RyZWUgewogICAgTm9kZSBUW01BWF07CiAgICBpbnQgbjsKCiAgICB2b2lkIGNsZWFyKGludCBuLCBpbnQgKlApIHsKICAgICAgICB0aGlzLT5uID0gbjsgICAgICAgICAgICAgICAKICAgICAgICAKICAgICAgICBidWlsZCgxLCAxLCBuLCBQKTsKICAgIH0KICAgIAogICAgTm9kZSBidWlsZChpbnQgdiwgaW50IGEsIGludCBiLCBpbnQgKlApIHsKICAgICAgICBpZiAoYT09YikKICAgICAgICAgICAgcmV0dXJuIFRbdl0gPSBOb2RlKDEtUFthXSwgUFthXSk7CiAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gVFt2XSA9IAogICAgICAgICAgICAgICAgYnVpbGQoMip2LCBhLCAoYStiKS8yLCBQKSArIAogICAgICAgICAgICAgICAgYnVpbGQoMip2KzEsIChhK2IpLzIrMSwgYiwgUCk7CiAgICB9CiAgICAKICAgIE5vZGUgdXBkYXRlKGludCB2LCBpbnQgYSwgaW50IGIsIGludCBpLCBpbnQgaiwgaW50IGNhcnJ5LCBpbnQgaW5jcmVtZW50KSB7CiAgICAgICAgVFt2XS5jaGFuZ2UoY2FycnkpOwoKICAgICAgICBpZiAoaT5iIHx8IGo8YSkKICAgICAgICAgICAgcmV0dXJuIE5vZGUoMCk7CgogICAgICAgIGlmIChpPD1hICYmIGI8PWopIAogICAgICAgICAgICByZXR1cm4gVFt2XS5jaGFuZ2UoaW5jcmVtZW50KTsKICAgICAgICAKICAgICAgICBOb2RlIGFuc3dlciA9IAogICAgICAgICAgICB1cGRhdGUodioyLCBhLCAoYStiKS8yLCBpLCBqLCBUW3ZdLnBlbmRpbmcsIGluY3JlbWVudCkgKyAKICAgICAgICAgICAgdXBkYXRlKHYqMisxLCAoYStiKS8yKzEsIGIsIGksIGosIFRbdl0ucGVuZGluZywgaW5jcmVtZW50KTsKICAgICAgICAKICAgICAgICBUW3ZdID0gVFt2KjJdICsgVFt2KjIrMV07CiAgICAgICAgCiAgICAgICAgcmV0dXJuIGFuc3dlcjsKICAgIH0KCiAgICBOb2RlIHVwZGF0ZShpbnQgaSwgaW50IGosIGludCBpbmMpIHsKICAgICAgICByZXR1cm4gdXBkYXRlKDEsIDEsIG4sIGksIGosIDAsIGluYyk7CiAgICB9CgogICAgTm9kZSBxdWVyeShpbnQgaSwgaW50IGopIHsKICAgICAgICByZXR1cm4gdXBkYXRlKGksIGosIDApOwogICAgfQoKfTsKClNlZ3RyZWUgVDsKaW50IFBbTUFYXTsKc3RyaW5nIHM7CgppbnQgbWFpbigpIHsKICAgIGludCBjYXNlczsgY2luID4+IGNhc2VzOwogICAgCiAgICBmb3IoaW50IHR0PTE7IHR0PD1jYXNlczsgdHQrKykgewogICAgICAgIGNvdXQgPDwgIkNhc2UgIiA8PCB0dCA8PCAiOiIgPDwgZW5kbDsKCiAgICAgICAgaW50IG07IGNpbiA+PiBtOwogICAgICAgIGludCBuID0gMDsKICAgICAgICBmb3IoaW50IGk9MDsgaTxtOyBpKyspIHsKICAgICAgICAgICAgaW50IHQ7CiAgICAgICAgICAgIGNpbiA+PiB0ID4+IHM7CiAgICAgICAgICAgIGZvcihpbnQgaj0wOyBqPHQ7IGorKykgewogICAgICAgICAgICAgICAgZm9yKGludCBrPTA7IGs8cy5zaXplKCk7IGsrKykgewogICAgICAgICAgICAgICAgICAgIFBbKytuXSA9IHNba10tJzAnOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIFQuY2xlYXIobiwgUCk7CiAgICAgICAgCiAgICAgICAgaW50IHE7IGNpbiA+PiBxOwogICAgICAgIGludCBxdWVyeSA9IDA7CiAgICAgICAgd2hpbGUocS0tKSB7CiAgICAgICAgICAgIGNoYXIgY21kOyBpbnQgYSwgYjsKICAgICAgICAgICAgY2luID4+IGNtZCA+PiBhID4+IGI7CiAgICAgICAgICAgIGErKzsgYisrOwogICAgICAgICAgICBpZiAoY21kID09ICdGJykgewogICAgICAgICAgICAgICAgVC51cGRhdGUoYSwgYiwgMSk7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoY21kID09ICdFJykgewogICAgICAgICAgICAgICAgVC51cGRhdGUoYSwgYiwgMik7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoY21kID09ICdJJykgewogICAgICAgICAgICAgICAgVC51cGRhdGUoYSwgYiwgMyk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBOb2RlIG5vZGUgPSBULnF1ZXJ5KGEsIGIpOwogICAgICAgICAgICAgICAgY291dCA8PCAiUSIgPDwgKytxdWVyeSA8PCAiOiAiIDw8IG5vZGUuYiA8PCBlbmRsOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9