A Super Hero
Difficulty: Hard

Ma5termind is crazy about Action Games. He just bought a new one and got down to play it. Ma5termind usually finishes all the levels of a game very fast. But, This time however he got stuck at the very first level of this new game. Can you help him play this game.  

To finish the game, Ma5termind has to cross $N$ levels. At each level of the game, Ma5termind has to face $M$ enemies. Each enemy has its associated power $P$ and some number of bullets $B$. To knock down an enemy, Ma5termind needs to shoot him with one or multiple bullets whose collective count is equal to the power of the enemy. If Ma5termind manages to knock down any one enemy at a level, the rest of them run away and the level is cleared.   

**Here comes the challenging part of the game.**  
Ma5termind acquires all the bullets of an enemy once he has knocked him down. Ma5termind can use the bullets acquired after killing an enemy at $i^{th}$ level only till the $(i+1)^{th}$ level.  

However, the bullets Ma5termind carried before the start of the game can be taken forward and can be used to kill more enemies.  

Now, Ma5termind has to guess the minimum number of bullets he must have before the start of the game so that he clears all the $N$ levels successfully.  


**NOTE**  

1. Bullets carried before the start of the game can be used to kill an enemy at any level.  
2. One bullet decreases the power of an enemy by 1 Unit.  
3. For better understanding of the problem look at the sample testcases.  



Input Format:
First line of input contains a single integer $T$ denoting the number of test cases.  
First line of each test case contains two space separated integers $N$ and $M$ denoting the number of levels and number of enemies at each level respectively.  
Each of next $N$ lines of a test case contain $M$ space separated integers, where $j^{th}$ integer in the $i^{th}$ line denotes the power $P$ of $j^{th}$ enemy on the $i^{th}$ level.  
Each of the next $N$ lines of a test case contains $M$ space separated integers, where $j^{th}$ integer in the $i^{th}$ line denotes the number of bullets $B$ $j^{th}$ enemy of $i^{th}$ level has.  

**Constraints**  
$1 \le T \le 100$  
$1 \le N \le 100$  
$1 \le M \le 5 \times 10^5$  
$1 \le P,B \le 1000$  
For each test file, sum of $N\times M$ over all the test cases does not exceed $5 \times 10^5$.   



Output Format:
For each test case, print the required answer.  

Sample Input:
	2
	3 3
	3 2 1 
	1 2 3
	3 2 1
	1 2 3
	3 2 1
	1 2 3
	3 3 
	3 2 5 
	8 9 1 
	4 7 6 
	1 1 1 
	1 1 1 
	1 1 1 


Sample Output:
	1
    5	

Python 3 starter code:
#
# Complete the 'superHero' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. 2D_INTEGER_ARRAY power
#  2. 2D_INTEGER_ARRAY bullets
#

def superHero(power, bullets):
    # Write your code here




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

#define pb push_back

#define mp make_pair

#define all(x) x.begin(),x.end()

#define PII pair<int,int>

#define ft first 

#define sd second

#define inf 10000000

using namespace std;

int main(){

    int t;
    cin >> t;
    while(t--){
        int n,m;
        cin >> n >> m;
        int P[n][m],W[n][m],MW[n][m];;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>P[i][j];
                MW[i][j]=inf;
            }
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>W[i][j];
            }
        }
    
        int ans;
        
        vector <pair<int,int> > MS;

        for(int i=0;i<m;i++) 
        {
            MW[0][i]=P[0][i];               
            MS.push_back(mp(W[0][i],MW[0][i]));
        }
    
        sort(all(MS));

        for(int i=1;i<n;i++){

            vector<PII > t;

            for(int j=0;j<m;j++){
            
                t.push_back(mp(P[i][j],j));

            }
            sort(all(t));

            int k=0,l=0,maxx=inf;
            
            while(l<m){

            while(k<m&&MS[k].ft<=t[l].ft){
            
                maxx=min(maxx,MS[k].sd-MS[k].ft);                    
                k++;            
            }    
            
            MW[i][t[l].sd]=min(MW[i][t[l].sd],maxx+P[i][t[l].sd]);    
            l++;
        }    
        
        k=m-1;
        l=m-1;
        maxx=inf;
        while(l>=0){
            
            while(k>=0&&MS[k].ft>=t[l].ft)
            {
                maxx=min(maxx,MS[k].sd);
                k--;    
            }
            MW[i][t[l].sd]=min(MW[i][t[l].sd],maxx);
            l--;            
        }
        
        MS.clear();
        
        for(int j=0;j<m;j++){
            MS.push_back(mp(W[i][j],MW[i][j]));
        }
        
        sort(all(MS));
    }

    ans=inf;
    for(int i=0;i<m;i++) 
        ans=min(ans,MW[n-1][i]);
    
    cout << ans << endl;
}
    
return 0;
}
```