#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define PI acos(-1.0)
#define inf 0x7f7f7f7f
#define mod 1000000007
//////////////////////////////////////////////
//////////////////////////////////////////////
#define L n*2
#define R n*2+1
#define M (int)((b+e)/2)
#define Node tree[n]
#define Right tree[R]
#define Left tree[L]
///////////////////////////////////////////////
///////////////////////////////////////////////
struct info{int s,p;} tree[4*1024006];
string s;
void Build(int n, int b, int e)
{
if(b==e){
Node.s = s[b]-'0';
Node.p = -1;
return;
}
Build(L,b,M);
Build(R,M+1,e);
Node.s = Left.s + Right.s;
}
void Update(int n, int b, int e, int x, int y, int v)
{
if(Node.p!=-1){
if(Node.p==2) Node.s = (e-b+1) - Node.s;
else Node.s = (e-b+1) * Node.p;
if(b!=e){
if(Node.p==2){
if(Left.p==0) Left.p=1;
if(Left.p==1) Left.p=0;
if(Left.p==2) Left.p=-1;
if(Left.p==-1) Left.p=2;
if(Right.p==0) Right.p=1;
if(Right.p==1) Right.p=0;
if(Right.p==2) Right.p=-1;
if(Right.p==-1) Right.p=2;
}
else Left.p = Right.p = Node.p;
}
Node.p = -1;
}
if(b>y || e<x) return;
if(b>=x && e<=y){
if(v==2) Node.s = (e-b+1) - Node.s;
else Node.s = (e-b+1)*v;
if(v==2){
if(Left.p==0) Left.p=1;
if(Left.p==1) Left.p=0;
if(Left.p==2) Left.p=-1;
if(Left.p==-1) Left.p=2;
if(Right.p==0) Right.p=1;
if(Right.p==1) Right.p=0;
if(Right.p==2) Right.p=-1;
if(Right.p==-1) Right.p=2;
return;
}
Left.p = Right.p = v;
return;
}
Update(L,b,M,x,y,v);
Update(R,M+1,e,x,y,v);
Node.s = Left.s + Right.s;
}
int Find(int n, int b, int e, int x, int y)
{
if(Node.p!=-1){
if(Node.p==2) Node.s = (e-b+1) - Node.s;
else Node.s = (e-b+1) * Node.p;
if(b!=e){
if(Node.p==2){
if(Left.p==0) Left.p=1;
if(Left.p==1) Left.p=0;
if(Left.p==2) Left.p=-1;
if(Left.p==-1) Left.p=2;
if(Right.p==0) Right.p=1;
if(Right.p==1) Right.p=0;
if(Right.p==2) Right.p=-1;
if(Right.p==-1) Right.p=2;
}
else Left.p = Right.p = Node.p;
}
Node.p = -1;
}
if(b>y || e<x) return 0;
if(b>=x && e<=y) return Node.s;
int f1 = Find(L,b,M,x,y);
int f2 = Find(R,M+1,e,x,y);
return f1+f2;
}
int main()
{
//freopen("Pin.txt","r",stdin);
//freopen("P.txt","w",stdout);
int test,cs=1;
int n,m,q,i,j;
scanf("%d",&test);
while(test--){
printf("Case %d:\n",cs++);
scanf("%d",&m);
s="0";
while(m--){
int t;
string str;
scanf("%d",&t);
cin>>str;
while(t--) s+=str;
}
n = s.size()-1;
//cout<<s<<endl;
//cout<<n<<endl;
for(i=0;i<=4*n;i++) tree[i].s=0 , tree[i].p = -1;
Build(1,1,n);
scanf("%d",&q);
int qs=1;
while(q--){
int x,y;
char ch;
getchar();
scanf("%c%d%d",&ch,&x,&y);
x++;
y++;
if(ch=='F') Update(1,1,n,x,y,1);
if(ch=='E') Update(1,1,n,x,y,0);
if(ch=='I') Update(1,1,n,x,y,2);
if(ch=='S') printf("Q%d: %d\n",qs++,Find(1,1,n,x,y));
}
}
return 0;
}
/*
2
2
5
10
2
1000
5
F 0 17
I 0 5
S 1 10
E 4 9
S 2 10
3
3
1
4
0
2
0
2
I 0 2
S 0 8
*/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBQSSBhY29zKC0xLjApCiNkZWZpbmUgaW5mIDB4N2Y3ZjdmN2YKI2RlZmluZSBtb2QgMTAwMDAwMDAwNwovLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vCi8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vL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sKCiAgICAgICAgICAgICAgICBpZihSaWdodC5wPT0wKSBSaWdodC5wPTE7CiAgICAgICAgICAgICAgICBpZihSaWdodC5wPT0xKSBSaWdodC5wPTA7CiAgICAgICAgICAgICAgICBpZihSaWdodC5wPT0yKSBSaWdodC5wPS0xOwogICAgICAgICAgICAgICAgaWYoUmlnaHQucD09LTEpIFJpZ2h0LnA9MjsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIExlZnQucCA9IFJpZ2h0LnAgPSBOb2RlLnA7CiAgICAgICAgfQogICAgICAgIE5vZGUucCA9IC0xOwogICAgfQogICAgaWYoYj55IHx8IGU8eCkgcmV0dXJuOwogICAgaWYoYj49eCAmJiBlPD15KXsKICAgICAgICBpZih2PT0yKSBOb2RlLnMgPSAoZS1iKzEpIC0gTm9kZS5zOwogICAgICAgIGVsc2UgTm9kZS5zID0gKGUtYisxKSp2OwogICAgICAgIGlmKHY9PTIpewogICAgICAgICAgICBpZihMZWZ0LnA9PTApIExlZnQucD0xOwogICAgICAgICAgICBpZihMZWZ0LnA9PTEpIExlZnQucD0wOwogICAgICAgICAgICBpZihMZWZ0LnA9PTIpIExlZnQucD0tMTsKICAgICAgICAgICAgaWYoTGVmdC5wPT0tMSkgTGVmdC5wPTI7CgogICAgICAgICAgICBpZihSaWdodC5wPT0wKSBSaWdodC5wPTE7CiAgICAgICAgICAgIGlmKFJpZ2h0LnA9PTEpIFJpZ2h0LnA9MDsKICAgICAgICAgICAgaWYoUmlnaHQucD09MikgUmlnaHQucD0tMTsKICAgICAgICAgICAgaWYoUmlnaHQucD09LTEpIFJpZ2h0LnA9MjsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBMZWZ0LnAgPSBSaWdodC5wID0gdjsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBVcGRhdGUoTCxiLE0seCx5LHYpOwogICAgVXBkYXRlKFIsTSsxLGUseCx5LHYpOwogICAgTm9kZS5zID0gTGVmdC5zICsgUmlnaHQuczsKfQoKaW50IEZpbmQoaW50IG4sIGludCBiLCBpbnQgZSwgaW50IHgsIGludCB5KQp7CiAgICBpZihOb2RlLnAhPS0xKXsKICAgICAgICBpZihOb2RlLnA9PTIpIE5vZGUucyA9IChlLWIrMSkgLSBOb2RlLnM7CiAgICAgICAgZWxzZSBOb2RlLnMgPSAoZS1iKzEpICogTm9kZS5wOwogICAgICAgIGlmKGIhPWUpewogICAgICAgICAgICBpZihOb2RlLnA9PTIpewogICAgICAgICAgICAgICAgaWYoTGVmdC5wPT0wKSBMZWZ0LnA9MTsKICAgICAgICAgICAgICAgIGlmKExlZnQucD09MSkgTGVmdC5wPTA7CiAgICAgICAgICAgICAgICBpZihMZWZ0LnA9PTIpIExlZnQucD0tMTsKICAgICAgICAgICAgICAgIGlmKExlZnQucD09LTEpIExlZnQucD0yOwoKICAgICAgICAgICAgICAgIGlmKFJpZ2h0LnA9PTApIFJpZ2h0LnA9MTsKICAgICAgICAgICAgICAgIGlmKFJpZ2h0LnA9PTEpIFJpZ2h0LnA9MDsKICAgICAgICAgICAgICAgIGlmKFJpZ2h0LnA9PTIpIFJpZ2h0LnA9LTE7CiAgICAgICAgICAgICAgICBpZihSaWdodC5wPT0tMSkgUmlnaHQucD0yOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgTGVmdC5wID0gUmlnaHQucCA9IE5vZGUucDsKICAgICAgICB9CiAgICAgICAgTm9kZS5wID0gLTE7CiAgICB9CgogICAgaWYoYj55IHx8IGU8eCkgcmV0dXJuIDA7CiAgICBpZihiPj14ICYmIGU8PXkpIHJldHVybiBOb2RlLnM7CiAgICBpbnQgZjEgPSBGaW5kKEwsYixNLHgseSk7CiAgICBpbnQgZjIgPSBGaW5kKFIsTSsxLGUseCx5KTsKICAgIHJldHVybiBmMStmMjsKfQoKaW50IG1haW4oKQp7CiAgICAvL2ZyZW9wZW4oIlBpbi50eHQiLCJyIixzdGRpbik7CiAgICAvL2ZyZW9wZW4oIlAudHh0IiwidyIsc3Rkb3V0KTsKICAgIGludCB0ZXN0LGNzPTE7CiAgICBpbnQgbixtLHEsaSxqOwogICAgc2NhbmYoIiVkIiwmdGVzdCk7CiAgICB3aGlsZSh0ZXN0LS0pewogICAgICAgIHByaW50ZigiQ2FzZSAlZDpcbiIsY3MrKyk7CiAgICAgICAgc2NhbmYoIiVkIiwmbSk7CiAgICAgICAgcz0iMCI7CiAgICAgICAgd2hpbGUobS0tKXsKICAgICAgICAgICAgaW50IHQ7CiAgICAgICAgICAgIHN0cmluZyBzdHI7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsJnQpOwogICAgICAgICAgICBjaW4+PnN0cjsKICAgICAgICAgICAgd2hpbGUodC0tKSBzKz1zdHI7CiAgICAgICAgfQogICAgICAgIG4gPSBzLnNpemUoKS0xOwogICAgICAgIC8vY291dDw8czw8ZW5kbDsKICAgICAgICAvL2NvdXQ8PG48PGVuZGw7CiAgICAgICAgZm9yKGk9MDtpPD00Km47aSsrKSB0cmVlW2ldLnM9MCAsIHRyZWVbaV0ucCA9IC0xOwogICAgICAgIEJ1aWxkKDEsMSxuKTsKICAgICAgICBzY2FuZigiJWQiLCZxKTsKICAgICAgICBpbnQgcXM9MTsKICAgICAgICB3aGlsZShxLS0pewogICAgICAgICAgICBpbnQgeCx5OwogICAgICAgICAgICBjaGFyIGNoOwogICAgICAgICAgICBnZXRjaGFyKCk7CiAgICAgICAgICAgIHNjYW5mKCIlYyVkJWQiLCZjaCwmeCwmeSk7CiAgICAgICAgICAgIHgrKzsKICAgICAgICAgICAgeSsrOwogICAgICAgICAgICBpZihjaD09J0YnKSBVcGRhdGUoMSwxLG4seCx5LDEpOwogICAgICAgICAgICBpZihjaD09J0UnKSBVcGRhdGUoMSwxLG4seCx5LDApOwogICAgICAgICAgICBpZihjaD09J0knKSBVcGRhdGUoMSwxLG4seCx5LDIpOwogICAgICAgICAgICBpZihjaD09J1MnKSBwcmludGYoIlElZDogJWRcbiIscXMrKyxGaW5kKDEsMSxuLHgseSkpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9CgovKgoyCgoyCjUKMTAKMgoxMDAwCjUKRiAwIDE3CkkgMCA1ClMgMSAxMApFIDQgOQpTIDIgMTAKCjMKMwoxCjQKMAoyCjAKMgpJIDAgMgpTIDAgOAoqLwo=