LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion Find
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

How Far the AND Holds

MediumProblem #76
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Stretching the segment to the right can only ever switch bits off.

For an array a and two positions l≤r, write

f(l,r)=al​&al+1​&⋯&ar​

where & is the bitwise AND.

You are given the array and then q questions. Each one gives a starting position l and a threshold k, and asks for the largest r with l≤r≤n such that f(l,r)≥k - that is, how far to the right the segment can be stretched before its AND drops below k.

Input

First line of input will be a single integer t - the number of testcases.
The first line of each testcase contains a single integer n - the length of the array.
The second line contains n integers a1​,a2​,…,an​.
The third line contains a single integer q - the number of questions.
Each of the next q lines contains two integers l and k - the starting position and the threshold.

Positions are counted from 1.

Output

For every testcase print one line holding the answers to its q questions, in order, separated by spaces. If even f(l,l) is already below k, print −1 for that question.

Example

Input
3
5
15 14 17 42 34
3
1 7
2 15
4 5
5
7 5 3 1 7
4
1 7
5 7
2 3
2 2
7
19 20 15 12 21 7 11
4
1 15
4 4
7 12
5 7
Output
2 -1 5
1 5 2 2
2 6 -1 5

Take the first question of the first testcase. Starting at l=1, the ANDs are f(1,1)=15, f(1,2)=14, and f(1,3)=f(1,4)=f(1,5)=0, so r=2 is the furthest that stays at 7 or above. The second question starts at l=2, where a2​=14 is already below 15, so the answer is −1.

Constraints

1≤t≤104
1≤n≤2⋅105
1≤q≤105
1≤ai​≤109
1≤l≤n and 1≤k≤109
The sum of n over all testcases does not exceed 2⋅105, and so does the sum of q


This problem is based on Iva & Pav, problem 1878E from Codeforces Round 900, by Mike Mirzayanov and the Codeforces team. The statement here is our own.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit