Maximum Subarray Sum
Difficulty: Hard

We define the following:

- A *subarray* of array $a$ of length $n$ is a contiguous segment from $a[i]$ through $a[j]$ where $0 \le i \le j \lt n$.
- The *sum* of an array is the sum of its elements.

Given an $n$ element array of integers, $a$, and an integer, $m$, determine the maximum value of the sum of any of its subarrays modulo $m$. 

**Example**    
$a=[1,2,3]$    
$m=2$     

The following table lists all subarrays and their moduli:

```
		sum	%2
[1]		1	1
[2]		2	0
[3]		3	1
[1,2]		3	1
[2,3]		5	1
[1,2,3]		6	0
```
The maximum modulus is $1$.

**Function Description**

Complete the *maximumSum* function in the editor below.  

maximumSum has the following parameter(s):

- *long a[n]:* the array to analyze   
- *long m:* the modulo divisor   

**Returns**   
- *long:* the maximum (subarray sum modulo $m$)   

Input Format:
The first line contains an integer $q$, the number of queries to perform.

The next $q$ pairs of lines are as follows:

- The first line contains two space-separated integers $n$ and (long)$m$, the length of $a$ and the modulo divisor.  
- The second line contains $n$ space-separated long integers $a[i]$.

Output Format:

Sample Input:
<pre>
STDIN		Function
-----		--------
1			q = 1
5 7			a[] size n = 5, m = 7
3 3 9 9 5
</pre>

Sample Output:
	6

Python 3 starter code:
#
# Complete the 'maximumSum' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
#  1. LONG_INTEGER_ARRAY a
#  2. LONG_INTEGER m
#

def maximumSum(a, m):
    # Write your code here




Reference solution (C++):
###C++  
```cpp  
#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;

void solve()
{
    ll N,M;
    ll x,prefix=0,maxim=0;
    cin>>N>>M;
    set<ll> S;
    S.insert(0);
    for(int i=1;i<=N;i++){
        cin>>x;
        prefix = (prefix + x)%M;
        maxim = max(maxim,prefix);
        set<ll>::iterator  it = S.lower_bound(prefix+1);
        if( it != S.end() ){
            maxim = max(maxim,prefix - (*it) + M );
        }
        S.insert(prefix);
    }
    cout<<maxim<<endl;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)    solve();
    return 0;
}
```