Codovid Virus

1 seconds
128 MB
LOJ-1502 Udebug Debug
English

The world is stunned by the CodoVid virus, which is believed to be one of the deadliest viruses the world has ever seen. Due to its self-reproduction mechanism, the DNA sequence of the CodoVid virus grows at a high speed. The DNA sequence of CodoVid virus is a binary string consisting of 0’s and 1’s only. A codovid virus has an initial DNA sequence S1. Starting from day 2, each day codovid virus reproduces a new sequence and adds the new sequence to its DNA sequence.

The sequence of DNA added on the ith day, Si = Reverse(Si-1) + Flip(Si-1) + Reverse(Si-1),

  • The + symbol represents string concatenation operation.
  • R() function returns the characters in reverse order. i.e. R(1100) = 0011
  • F() function changes all the 0’s to 1’s and vice versa. i.e. F(1001) = 0110

So, on the first day the Codovid DNA sequence is S1. On the second day the sequence is S1+S2. On the third day, the sequence will become S1+S2+S3. So, it is an ever-growing sequence, S1+S2+S3+ ...

An example will make it clear.

Let’s assume the Initial CodoVid DNA sequence on Day 1, S1 = 100.

Sequence Value Explanation
S1 100 Initial Value
S2 001011001 R(100) + F(100) + R(100) = (001)+(011)+(001)
S3 100110100110100110100110100 R(001011001) + F(001011001) + R(001011001)

So, the first few characters of the CodoVid DNA sequence is: 100001011001100110100110100110100110100...

So, the 1st character of the codovid DNA sequence is 1. Similarly, 2nd, 3rd, 4th, 5th is 0, 6th character is 1 and 7th character is 0 and so on.

In this problem, you are given the initial DNA sequence of the Codovid virus, S1 and two integers L and R. You have to count the number of 1’s from the Lth character to the Rth character of the Codovid DNA sequence.

Input

The input begins with an integer T (1 ≤ T ≤ 100) denoting the number of test cases. T test cases follow. Every case starts with a non-empty string S1 (1 ≤ |S1| ≤ 104) in a single line. Next line of the input is an integer Q (1 ≤ Q ≤ 5000) denoting the number of queries. Then follow Q lines containing two integers L, R (1 ≤ L ≤ R ≤ 1017). You may safely assume that, Sum of length(S1) over all test cases ≤ 5 x 105.

Output

For each test case, Print the case number in the format Case X: in a single line , where X is the case number. Then, for each query of that test case, print the desired answer in a single line.

Please check the samples for further clarification.

Sample

Sample Input Sample Output

1 100 4 1 3 4 12 10 20 1 100

Case 1: 1 4 5 48