CF2193C.Replace and Sum

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Replace and Sum

Today, KQ has an exam at the Grail Academy. A strict teacher gave a task that KQ could not solve. He was given two arrays aa and bb of length nn. KQ is allowed to perform the following operations on the arrays:

  • Choose an index ii (1i<n1\le i\lt n) and replace aia_i with ai+1a_{i+1}.
  • Choose an index ii (1in1\le i\le n) and replace aia_i with bib_i.

Now he has qq queries. Each query is described by two numbers ll and rr. His task is to find the maximum value of the sum (al+al+1+al+2++ar)(a_l + a_{l+1} + a_{l+2} + \dots + a_r) for each query, if he can perform any number of operations on any elements of the array. Since he is not skilled enough for this, he asks for your help.

Input

Each test consists of several test cases. The first line contains one integer tt (1t1041\le t\le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn, qq (1n,q2105)(1\le n, q\le 2 \cdot 10^5).

The second line of each test case contains nn integers a1,a2,...,ana_1, a_2,...,a_n (1ai104)(1\le a_i\le 10^4).

The third line of each test case contains nn integers b1,b2,...,bnb_1, b_2,...,b_n (1bi104)(1\le b_i\le 10^4).

The following qq lines contain two integers ll and rr (1lrn)(1\le l\le r\le n).

It is guaranteed that the sum of the values of nn and the sum of the values of qq across all test cases do not exceed 21052 \cdot 10^5.

Output

For each test case, output qq numbers separated by spaces — the maximum values of the sums (al+al+1+al+2++ar)(a_l + a_{l+1} + a_{l+2} + \dots + a_r).

Note

Consider the first test case. Replace a3a_3 with b3b_3, a=[3,2,3]a = [3, 2, 3]. Replace a2a_2 with a3a_3, a=[3,3,3]a = [3, 3, 3]. The sum a1+a2+a3=9a_1 + a_2 + a_3 = 9.

Samples

4
3 1
3 2 1
1 2 3
1 3
1 1
1
2
1 1
3 2
6 7 5
9 6 8
1 2
2 3
4 3
4 3 2 1
5 1 3 1
1 2
2 4
3 4
9
2
17 16
8 7 4

在线编程 IDE

建议全屏模式获得最佳体验