eolymp
bolt
Try our new interface for solving problems
Problems

Ahoy, Pirates!

Ahoy, Pirates!

In the ancient pirate ages, the Pirate Land was divided into two teams of pirates, namely, the Buccaneer and the Barbary pirates. Each pirate's team was not fixed, sometimes the opponent pirates attacked and he was taken away to the other pirate team. All on a sudden a magician appeared in the Pirate Land, where he was making transition of pirates from their team to other team at his own will. Of course, handy spells were used. The process of changing team was known as mutating.

There were n pirates and all of the pirates have a unique id from 0 to n - 1. The great magician could mutate a bunch of pirates with consecutive id's to another one.

Suppose there were 100 pirates in the pirate land and all of them were Barbary pirates, then the magician could cast a spell to change pirates with id's from 10 to 33 to Buccaneer pirates. Then the whole pirate land would have 24 Buccaneer and 76 Barbary pirates.

The magician was very fast casting the spell. Once, God started to dislike this. God had favor for the Buccaneer pirates and God asked the magician "Tell me, how many of the pirates of index from 2 to 30 are Buccaneer pirates?". Now the magician was puzzled as he was only efficient in casting spells, not in counting :-)

Being clever enough, the magician captured a clever man from the Earth Land. And unfortunately its you! Now you have to answer the Gods questions.

Input

The first line of input will contain number of test cases t. For each test case:

The first part of the description will be of the pirate land. There could be up to n (1n1024000) pirates. Each pirate is either assigned to Buccaneer or Barbary Pirate. Buccaneer pirates are described by '1' (ONE) and Barbary pirates are described by '0' (ZERO). You have to build a string of the pirates description. Each case starts with an integer m (m100), where m pair lines follow. In each pair of lines (we call it a set), first has an integer t (t200) and next one has a nonempty string Pirates (consisting of 0 and 1, 0 for Barbary, 1 for Buccaneer, has maximum length of 50). For each pair concatenate the string Pirates, t times. Concatenate all the resulting m sets of strings to build the pirate description. The final concatenated string describes the pirates from index 0 to end (n - 1 for n pirates).

Now the next part of the input will contain queries. First line of next part has an integer q describing number of queries. Each subsequence q (1q1000) lines describe each query. Each query has a string F or E or I or S and two integers, a and b denoting indexes. The meaning of the query string are follows:

F a b means, mutate the pirates from index a to b to Buccaneer Pirates.

E a b means, mutate the pirates from index a to b to Barbary Pirates.

I a b means, mutate the pirates from index a to b to inverse pirates.

S a b means "God's query" God is asking a question: "Tell me, how many Buccaneer pirates are there from index a to b?"

(ab, 0a < n, 0b < n, index range are inclusive)

Output

For each test print the case number as the sample output suggests. Then for each of God's query, output the query number, colon (:) and a space and the answer to the query as the sample suggest.

Time limit 1 second
Memory limit 122.17 MiB
Input example #1
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
Output example #1
Case 1:
Q1: 5
Q2: 1
Case 2:
Q1: 0