Under construction

Still being built. Things may change

Square Game

Time limit1sMemory limit1024 MB

Summary
For each query range of a sequence, find the maximum number of square operations that replace k copies of k with one k squared.
Level

Hard8 of 10

Topics
Divide and conquer, Greedy, Hash map
Solved
No attempts yet

Problem

For an integer kk at least 2, an operation that replaces kk copies of kk with one k2k^2 is called a "square".

You are given nn integers a1,a2,…,ana_1, a_2, \ldots, a_n. Write a program that processes the following query.

  • ll rr: Find the maximum number of squares that can be performed on the sequence al,al+1,…,ara_l, a_{l+1}, \ldots, a_r, and print it.

For example, in the sequence 2,4,2,4,2,22, 4, 2, 4, 2, 2, replacing two 2s with 4 twice gives 4,4,4,44, 4, 4, 4. Then the four 4s can be replaced with 16. That makes 3 squares in total, so the maximum number of squares for this sequence is 3.

Input

The first line contains TT, the number of test cases. The TT test cases follow. (1≤T≤321 \le T \le 32)

The first line of each test case contains nn. (1≤n≤50 0001 \le n \le 50\,000)

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, separated by spaces. (1≤ai≤1 000 000 0001 \le a_i \le 1\,000\,000\,000)

The next line contains QQ, the number of queries. (1≤Q≤50 0001 \le Q \le 50\,000)

Each of the next QQ lines contains two integers lil_i and rir_i, separated by a space, which describe the ii-th query. (1≤li≤ri≤n1 \le l_i \le r_i \le n)

Output

For each test case, print "Case #CC" on the first line, where CC is the test case number.

The next QQ lines each contain the maximum number of squares that can be performed for the corresponding query.

Examples1

  1. Example 1

    Input
    1
    12
    2 3 2 3 4 3 2 3 2 1 2 2
    3
    1 12
    2 7
    3 8
    
    Expected output
    Case #1
    5
    2
    2