#include <bits/stdc++.h>
#define SET 1
#define FLIP 2
#define N (1024*1024+1)
#define M 101
int n, T, m, nQuery;
int cStr[M];
int lStr[M];
int t[2 * N];
int setTo[N];
int mode[N];
int tsize[2 * N];
char str[M][M];
void build()
{
for (int i = n; i < 2 * n; i++)
tsize[i] = 1;
for (int i = n - 1; i > 0; i--)
{
tsize[i] = tsize[i << 1] + tsize[i << 1 | 1];
mode[i] = setTo[i] = 0;
t[i] = t[i << 1] + t[i << 1 | 1];
}
mode[0]=setTo[0]=t[0]=tsize[0]=0;
}
void apply (int i, int modo, int val)
{
if(i==0) return;
if (modo == SET)
{
t[i] = val * tsize[i];
if(i<n){
setTo[i] = val;
mode[i] = SET;
}
}
else if (modo == FLIP)
{
t[i] = tsize[i] - t[i];
if(i<n){
if(mode[i]==SET) setTo[i]=!setTo[i];
else if(mode[i]==FLIP) mode[i]=0;
else mode[i] = FLIP;
}
}
}
void pull (int i)
{
while (i >>= 1)
{
if (mode[i] == 0)
t[i] = t[i << 1] + t[i << 1 | 1];
else if (mode[i] == SET)
t[i] = setTo[i] * tsize[i];
else if (mode[i] == FLIP)
t[i] = tsize[i] - t[i << 1] - t[i << 1 | 1];
}
}
void push (int i)
{
for (int s = 32; s >0 ; s--)
{
int p = i >> s;
if(p==0) continue;
if (mode[p] != 0)
{
apply(p << 1, mode[p], setTo[p]);
apply(p << 1 | 1, mode[p], setTo[p]);
setTo[p] = mode[p] = 0;
}
}
}
void update (int modo, int l, int r, int val)
{
int l0 = l + n, r0 = r + n;
push(l0);
push(r0);
for (l += n, r += n; l <= r; l>>=1, r>>=1)
{
if ( l & 1 ) apply(l++, modo, val);
if (!(r & 1)) apply(r--, modo, val);
}
pull(l0);
pull(r0);
}
int query (int l, int r)
{
int res = 0;
push(l + n), push(r + n);
for (l += n, r += n; l <= r; l>>=1, r>>=1)
{
if ( l & 1 ) res += t[l++];
if (!(r & 1)) res += t[r--];
}
return res;
}
int main()
{
scanf("%d", &T);
for (int z = 1; z <= T; z++)
{
scanf("%d", &m);
n = 0;
for (int i = 0; i < m; i++)
{
scanf("%d %s", &cStr[i], &str[i]);
lStr[i] = strlen(str[i]);
n+=lStr[i]*cStr[i];
}
for (int i = 0,pos=0; i < m; i++)
for (int j = 0; j < cStr[i]; j++)
for (int k = 0; k < lStr[i]; k++)
t[n+pos++] = str[i][k] - '0';
build();
printf("Case %d:\n", z);
scanf("%d", &nQuery);
int nQuestion = 1;
for (int i = 0; i < nQuery; i++)
{
char c[32];
int l, r;
scanf("%s %d %d", &c, &l, &r);
switch (c[0])
{
case 'F':
update(SET, l, r, 1);
break;
case 'E':
update(SET, l, r, 0);
break;
case 'I':
update(FLIP, l, r, 0);
break;
case 'S':
printf("Q%d: %d\n", nQuestion++, query(l, r));
break;
}
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgU0VUIDEKI2RlZmluZSBGTElQIDIKI2RlZmluZSBOICgxMDI0KjEwMjQrMSkKI2RlZmluZSBNIDEwMQppbnQgbiwgVCwgbSwgblF1ZXJ5OwppbnQgY1N0cltNXTsKaW50IGxTdHJbTV07CmludCB0WzIgKiBOXTsKaW50IHNldFRvW05dOwppbnQgbW9kZVtOXTsKaW50IHRzaXplWzIgKiBOXTsKY2hhciBzdHJbTV1bTV07Cgp2b2lkIGJ1aWxkKCkKewoJZm9yIChpbnQgaSA9IG47IGkgPCAyICogbjsgaSsrKQoJCXRzaXplW2ldID0gMTsKCWZvciAoaW50IGkgPSBuIC0gMTsgaSA+IDA7IGktLSkKCXsKCQl0c2l6ZVtpXSA9IHRzaXplW2kgPDwgMV0gKyB0c2l6ZVtpIDw8IDEgfCAxXTsKCQltb2RlW2ldID0gc2V0VG9baV0gPSAwOwoJCXRbaV0gPSB0W2kgPDwgMV0gKyB0W2kgPDwgMSB8IDFdOwoJfQoJbW9kZVswXT1zZXRUb1swXT10WzBdPXRzaXplWzBdPTA7Cn0KCnZvaWQgYXBwbHkgKGludCBpLCBpbnQgbW9kbywgaW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