Given an array of **n** integers and **q** queries, each query having a range from **l** to **r**. Find the maximum prefix-sum for the range **l** to **r**.

**NOTE: **Assume indexing from 0.

```
Input: a[] = {-1, 2, 3, -5}
q = 2
l = 0, r = 3
l = 1, r = 3
Output: 4
5
Explanation:- The range (0, 3) in the 1st query has
[-1, 2, 3, -5], since it is prefix,
we have to start from -1. Hence, the
max prefix sum will be -1 + 2 + 3 = 4.
The range (1, 3) in the 1st query has
[2, 3, -5], since it is prefix, we
have to start from 2. Hence, the max
prefix sum will be 2 + 3 = 5.
```

**Input:**

The first line consists of an integer **T** i.e number of test cases. The first line of each test case consists of two integers **n** and **q**.Then next line consists of **n** spaced integers. The last line of each test case consists of **q** pairs of integers representing **l** and **r**.

**Output:**

Print the required answer.

**Constraints: **

1<=T<=100

1<=n,q,a[i]<=10^{4}

**Example:
Input:**

2

4 2

-1 2 3 -5

0 3 1 3

8 1

-2 -3 4 -1 -2 1 5 -3

1 7

**Output:**

4 5

4

Author: saksham0751

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.