The Return of the Wayward Parrot#4498

The Return of the Wayward Parrot

5 seconds
256 megabytes
Medium

In this task, Wolf wants to teach Kesha how to count by playing a game. Wolf writes down a sequence of numbers and gives Kesha pairs of numbers, and . For each pair, Kesha must find a subarray (a contiguous segment) within the range such that the sum of its elements,

a_i + a_{i+1} + \ldots + a_{j-1} + a_j,

where and are between and (l \leq i \leq j \leq r), is maximized. Each morning, Wolf may change (or leave unchanged) some numbers in the array, and each evening, he presents Kesha with one or more similar tasks.

Your task is to help Kesha by writing a program that finds the maximum subarray sum under these conditions.

Input

The first line contains the numbers and (1 \leq N, D \leq 10^6)—the length of the sequence and the number of days Wolf teaches Kesha. The second line contains numbers, representing the initial state of the sequence. Each number does not exceed 10^9 in absolute value.

Then follows (1 \leq M \leq 10^5)—the number of days Wolf modifies the sequence. The next lines each contain three numbers: , , and , where is the day number (1 \leq d \leq D), is the -based position of the element being changed (1 \leq x \leq N), and is the new value of that element. The days are given in chronological order. On any given day, Wolf may change multiple elements.

Then follows the number (1 \leq K \leq 10^5)—the number of days Kesha asks for your help. Each query consists of two numbers, and (1 \leq l \leq r \leq N), which define the boundaries of the segment where you need to calculate the maximum subarray sum. The day when Kesha makes each query is computed as:

d = (z + i) \bmod D + 1,

where is the answer to the previous query (for the first query, let z = 0), and is the current query number (starting from ). All indices in queries and updates are -based.

Output

Output lines, each containing one number—the answer to the corresponding query.

Examples

Example 1

Loading
Loading