You are given a 0-indexed integer array nums. Initially on minute 0, the array is unchanged. Every minute, the leftmost element in nums is removed until no elements remain. Then, every minute, one element is appended to the end of nums, in the order they were removed in, until the original array is restored. This process repeats indefinitely.
For example, the array [0,1,2] would change as follows: [0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → [1,2] → [2] → [] → [0] → [0,1] → [0,1,2] → ...
You are also given a 2D integer array queries of size n where queries[j] = [timej, indexj]. The answer to the jth query is:
nums[indexj]ifindexj < nums.lengthat minutetimej-1ifindexj >= nums.lengthat minutetimej
Return an integer array ans of size n where ans[j] is the answer to the jth query.
Input & Output
Constraints
- 1 ≤ nums.length ≤ 5000
- -109 ≤ nums[i] ≤ 109
- 1 ≤ queries.length ≤ 5000
- 0 ≤ timej ≤ 109
- 0 ≤ indexj ≤ nums.length - 1