Chef organised a chess tournament, which spanned over $M$ months. There were $N$ players, and player $i$ was rated $R_i$ before the start of the tournament. To see the progress of the players, he noted their rating changes at the end of each month.
After the tournament, FIDE asked Chef to find the number of players whose peak rating and peak ranking did not occur in the same month. In other words, Chef was asked to find the ratings and ranking of each player after each of the $M$ months. Then, using this data, he should find the number of players, such that the month in which they achieved their highest rating over all the months, was different from the month in which they achieved their best rank (based on ratings), over all the months. Note that we do not consider the initial rating/ranking, but only the rating and rankings after each of the $M$ months. 
For a particular player, if there are multiple peak rating or peak ranking months, Chef was to consider the earliest of them. If multiple players had the same rating at the end of some month, they were to be given the same rank. For example, if there were $5$ players, and their ratings at the end of some month were $(2600$, $2590$, $2600$, $2600$ and $2590)$, players $1$, $3$ and $4$ were to be given the first rank, while players $2$ and $5$ should be given the fourth rank.
As Chef hates statistics, he asks you, his friend, to help him find this. Can you help Chef?

-----Input:-----
- The first line contains an integer $T$, the number of test cases.
- The first line of each test case contains two space-separated integers $N$ and $M$, the number of players and the number of months that the tournament spanned over.
- The second line of each test case contains $N$ space-separated integers, $R_1, R_2, \ldots, R_N$ denoting the initial ratings of the players, i.e., their ratings before the start of the tournament.
- The next $N$ lines each contain $M$ space-separated integers. The $j^{th}$ integer of the $i^{th}$ line, $C_{i,j}$ denotes the rating change of the $i^{th}$ player after the $j^{th}$ month.

-----Output:-----
For each test case, print the number of players whose peak ratings did not occur in the same month as their peak ranking, in a new line.

-----Constraints-----
- $1 \le T \le 10$
- $1 \le N,M \le 500$
- $1800 \le R_i \le 2800$
- $-20 \le C_{i,j} \le 20$

-----Subtasks-----
- 30 points : $1 \leq N,M \leq 50$
- 70 points : Original constraints.

-----Sample Input:-----
2
3 3
2500 2500 2520
10 -5 -20
10 15 20
-15 17 13
2 3
2125 2098
-20 10 -10
10 10 -20

-----Sample Output:-----
2
2

-----Explanation:-----
Test case 1:
- 
The ratings for player $1$ after each month are: $(2510$, $2505$ and $2485)$, while his rankings are first, third and third, respectively. Thus, his best rating and best ranking occur after the same month, i.e., after the first month.
- 
The ratings for player $2$ after each month are: $(2510$, $2525$ and $2545)$, while his rankings are first, first and first, respectively. His best rating occurs after the third month, while his best ranking occurs after the first month (we consider the first month even though his peak ranking is over all the months, because we consider only the earliest month where he attains the peak ranking).
- 
The ratings for player $3$ after each month are: $(2505$, $2522$ and $2535)$, while his rankings are third, second and second, respectively. His best rating occurs after the third month, while his best ranking occurs after the second month.
So there are two players ($2$ and $3$), whose peak ratings did not occur in the same month as their peak ranking, and hence the answer is 2.
Test case 2:
- 
The ratings for player $1$ after each month are: $(2105$, $2115$ and $2105)$, while his rankings are second, second and first, respectively. Best rating is after second month, but best ranking is after third month.
- 
The ratings for player $2$ after each month are: $(2108$, $2118$ and $2098)$, while his rankings are first, first and second, respectively. Best rating is after second month, but best ranking is after first month.
So there are two players ($1$ and $2$), whose peak ratings did not occur in the same month as their peak ranking, and hence the answer is 2.

Difficulty: interview

Starter code:

Solutions:
# cook your dish here
t=int(input())
for _ in range(t):
 n,m=list(map(int,input().split()))
 r=list(map(int,input().split()))
 rating=[[r[i]]*(m) for i in range(n)]
 ranking=[[0]*m for i in range(n)]
 for i in range(n):
  diff=list(map(int,input().split()))
  for j in range(m):
   rating[i][j]+=diff[j]
   if j+1<m:
    rating[i][j+1]=rating[i][j]
    
 for i in range(m):
  rate=[[j,rating[j][i]] for j in range(n)]
  rate=sorted(rate,key=lambda x: x[1],reverse=True)
  c=1
  gap=0
  for j in range(n):
   if j>0 and rate[j-1][1]==rate[j][1]:
    gap+=1
   if j>0 and rate[j-1][1]!=rate[j][1]:
    c+=1+gap
    gap=0
   ranking[rate[j][0]][i]=c 
   
 count=0
 for i in range(n):
  rate=rating[i].copy()
  i1=rate.index(max(rate))
  rank=ranking[i].copy()
  i2=rank.index(min(rank))
  if i1!=i2:
   count+=1
 print(count)


t=int(input())
for _ in range(t):
 n,m=list(map(int,input().split()))
 r=list(map(int,input().split()))
 rating=[[r[i]]*(m) for i in range(n)]
 ranking=[[0]*m for i in range(n)]
 for i in range(n):
  diff=list(map(int,input().split()))
  for j in range(m):
   rating[i][j]+=diff[j]
   if j+1<m:
    rating[i][j+1]=rating[i][j]
    
 for i in range(m):
  rate=[[j,rating[j][i]] for j in range(n)]
  rate=sorted(rate,key=lambda x: x[1],reverse=True)
  c=1
  gap=0
  for j in range(n):
   if j>0 and rate[j-1][1]==rate[j][1]:
    gap+=1
   if j>0 and rate[j-1][1]!=rate[j][1]:
    c+=1+gap
    gap=0
   ranking[rate[j][0]][i]=c 
   
 count=0
 for i in range(n):
  rate=rating[i].copy()
  i1=rate.index(max(rate))
  rank=ranking[i].copy()
  i2=rank.index(min(rank))
  if i1!=i2:
   count+=1
 #print(rating)
 #print(ranking)
 print(count)
 #print("---------------------------------------------------")
  
  
    
    


def check(rat,rank_mont):
 c=0
 for i in range(len(rank_mont)):
  if(rat[i]!=rank_mont[i]):
   c+=1
 return c

def ranking(rat,rank_min,mont,n,m):
 rat_sort=[]
 
 for i in range(m):
  rat_sort.append(sorted(rat[i],reverse=True,key=lambda x : x[0]))

 for i in range(m):
  r,j=1,0
  while(j<n):
   key=rat_sort[i][j][0]
   c=0
   while(j<n):
    if(key==rat_sort[i][j][0]):
     if(rank_min[rat_sort[i][j][1]-1]>r):
      rank_min[rat_sort[i][j][1]-1]=r
      mont[rat_sort[i][j][1]-1]=i+1
     c+=1
    else:
     break
    j+=1
   r+=c
   
for T in range(int(input())):
 n,m=list(map(int,input().split()))
 initial_rating=list(map(int,input().split()))
 rat=[list(map(int,input().split())) for i in range(n)]

 rat_player,m_rat_player=[],[]
 for i in range(n):
  m_rat,x=0,[]
  for j in range(m):
   x.append(initial_rating[i]+rat[i][j])
   initial_rating[i]=x[j]
   if(m_rat<x[j]):
    m_rat=x[j]
    month=j+1
  rat_player.append(x)
  m_rat_player.append(month)

 rat_month=[]
 for i in range(m):
  x=[]
  for j in range(n):
   x.append([rat_player[j][i],j+1])
  rat_month.append(x)

 rank_min,mont=[501]*(n),[0]*(n)

 ranking(rat_month,rank_min,mont,n,m)
 
 count=check(m_rat_player,mont)

 print(count)
 
   


# cook your dish here
#Author : Ashutosh Wagh, Codechef : ashutosh0903
for _ in range(int(input())):
 n,m=list(map(int,input().split()))
 r=list(map(int,input().split()))
 mat=[[] for i in range(n)]
 ranking=[[] for i in range(n)]
 for i in range(n):
  l=list(map(int,input().split()))
  cur = r[i]
  for j in l:
   cur+=j
   mat[i].append(cur)
 for i in range(m):
  ar=[]
  for j in range(n):
   ar.append([mat[j][i],j])
  ar.sort(reverse=True,key=lambda x:x[0])
  prev=-1
  r=1
  x=1
  for j in range(n):
   if ar[j][0]!=prev:
    r=x
   ranking[ar[j][1]].append(r)
   prev=ar[j][0]
   x+=1
 brt=[]
 brk=[]
 for i in range(n):
  brt.append(mat[i].index(max(mat[i])))
 for i in range(n):
  brk.append(ranking[i].index(min(ranking[i])))
 ans=0
 for i in range(n):
  if brk[i]!=brt[i]: ans+=1
 print(ans)



# cook your dish here
t=int(input())
for i in range(0,t):
 ans=0;
 nm=input().split()
 
 n,m=int(nm[0]),int(nm[1])
 rows,cols=m,n
 a=[[(0,0) for i in range(cols)] for j in range(rows)]

 max_rating_month=[0 for i in range(cols)]
 curr_rating=list(map(int,input().strip().split()))[:n] 


 for i in range(0,n):
  line=list(map(int,input().strip().split()))[:m]
  max=0
  for j in range(0,m):
   curr_rating[i]+=line[j]
   if curr_rating[i] >max :
    max_rating_month[i]=j
    max=curr_rating[i]
   a[j][i]=(i,curr_rating[i])
 ranking=[[0 for i in range(n)] for j in range(m)]
 for i in range(m):
  a[i].sort(key=lambda x:-x[1])
  rank=1
  ranking[i][a[i][0][0]]=1
  for j in range(1,n):
   if a[i][j-1][1]==a[i][j][1]:
    ranking[i][a[i][j][0]]=rank;
   else:
    ranking[i][a[i][j][0]]=j+1;
    rank=j+1;
 for i in range(n):
  best_rank=501
  best_rank_month=0
  for j in range(m):
   if ranking[j][i]<best_rank:
    best_rank=ranking[j][i]
    best_rank_month=j
  if best_rank_month!=max_rating_month[i]:
   ans+=1
 print(ans)
 


for _ in range(int(input())):
 n,m=list(map(int,input().split()))
 rating = list(map(int,input().split()))
 arr=[]
 for i in range(n):
  arr.append(list(map(int,input().split())))
 minrank = [float("inf")]*n
 index2 = [0]*n
 bestscore = [float("-inf")]*n
 index1=[0]*n
 for i in range(m):
  for j in range(n):
   rating[j]+=arr[j][i]
  st=sorted(rating,reverse=True)
  f=[]
  dindex = dict()
  for j in range(len(st)):
   if st[j] in dindex:
    continue
   
   dindex[st[j]]=j
  
  for j in range(len(rating)):
   f.append(dindex[rating[j]])
  
  for ii in range(len(f)):
   if minrank[ii]>f[ii]:
    minrank[ii]=f[ii]
    index1[ii]=i
  
   
  for ii in range(len(rating)):
   if bestscore[ii] < rating[ii]:
    bestscore[ii]=rating[ii]
    index2[ii]= i
 count=0
 
 for (i,j) in zip(index2,index1):
  if i!=j:
   count+=1
 print(count)
  
  


import sys
from collections import defaultdict
t=int(input())

for _ in range(t):
 n,m=list(map(int,input().split()))
 a=list(map(int,input().split()))
 adj=[]
 for _ in range(n):
  x=list(map(int,input().split()))
  adj.append(x)

 rating=defaultdict(lambda:-1)


 rank=[[-1 for i in range(m+1)]for j in range(n)]


 b=[[-1 for i in range(m+1)]for j in range(n)]
 for i in range(n):
  b[i][0]=a[i]
 for i in range(0,n):
  for j in range(1,m+1):
   b[i][j]=b[i][j-1]+adj[i][j-1]

 for j in range(1,m+1):
  g=[]
  d={}
  for i in range(n):
   g.append(b[i][j])
  z=sorted(g,reverse=True) 
  c=0
  for i in z:
   if(i not in d):
    c+=1
    d[i]=c
   else:
    c+=1
  for i in range(n):
   rank[i][j]=d[b[i][j]]

 for i in range(n):
  mx=-1
  for x in range(1,m+1):
   mx=max(mx,b[i][x])
  for j in range(1,m+1):
   if(b[i][j]==mx):
    rating[i]=j
    break
 #print(rating)
 #for i in b:
  #print(*i)
 c=0
 '''for i in b:
     print(*i)
    print()
    for i in rank:
     print(*i)'''
 
 for i in range(n):
  ans=[]
  mi=sys.maxsize
  for x in range(1,m+1):
   mi=min(mi,rank[i][x])
  for j in range(1,m+1):
   if(rank[i][j]==mi):
    if(j!=rating[i]):
     c+=1
    break

 print(c)




# cook your dish here
def __starting_point():
 t = int(input())
 while t>0:
  N, M = map(int, input().split())
  R = list(map(int, input().split()))
  Ratings = []
  Rankings = [[float("inf") for i in range(M)] for j in range(N)]
  for i in range(N):
   temp_ratings = list(map(int, input().split()))
   temp = []
   initial_rating = R[i]
   for rate in temp_ratings:
    temp.append(initial_rating+rate)
    initial_rating += rate
   Ratings.append(temp)
  for i in range(M):
   temp = []
   for j in range(N):
    temp.append(Ratings[j][i])
   temp = sorted(temp, reverse = True)
   dic = {}
   for j in range(N):
    if temp[j] not in dic:
     dic[temp[j]] = j+1
   for j in range(N):
    Rankings[j][i] = dic[Ratings[j][i]]
  
  cnt = 0
  for i in range(N):
   best_rating = 0
   best_rating_index = -1
   best_ranking = float("inf")
   best_ranking_index = -1
   for j in range(M):
    if Ratings[i][j] > best_rating:
     best_rating = Ratings[i][j]
     best_rating_index = j
    if Rankings[i][j] < best_ranking:
     best_ranking = Rankings[i][j]
     best_ranking_index = j
   if best_rating_index != best_ranking_index:
    cnt += 1
    
  print(cnt)
  
  t -= 1
__starting_point()

for i in range(int(input())):
 n, m = map(int, input().split())
 ratings = list(map(int, input().split()))
 ratings_after = [[] for _ in range(m)]
 max_rating_months = []
 
 for i in range(n):
  curr_changes = list(map(int, input().split()))
  best_rating = ratings[i] + curr_changes[0]
  curr_r, rat_month = best_rating, 0
  
  ratings_after[0].append(curr_r)
  for k in range(1, m):
   curr_r += curr_changes[k]

   if curr_changes[k] > 0:
    if curr_r > best_rating:
     best_rating = curr_r
     rat_month = k
   
   ratings_after[k].append(curr_r)
  max_rating_months.append(rat_month)
 
 rankings = [[] for _ in range(n)]
 
 for x in range(m):
  month = ratings_after[x]
  new_mon = sorted(month, reverse=True)
  
  for y in range(n):
   rankings[y].append(new_mon.index(month[y]))
 
 index, counter = 0, 0
 
 for player in rankings:
  best = player.index(min(player))
  
  if best != max_rating_months[index]:
   counter += 1
  index += 1

 print(counter)

import math

try:
 for _ in range(int(input())):
  n,m = list(map(int, input().split()))
  p = list(map(int, input().split()))
  points = [[0 for i in range(m+1)]for i in range(n)]
  for i in range(n):
   points[i][0] = p[i]
  mx = [[0,0] for i in range(n)]
  arr = [[3000,0] for i in range(n)]
  for i in range(n):
   lst = list(map(int,input().split()))
   for j in range(1, m+1):
    points[i][j] = points[i][j-1]+lst[j-1]
   points[i] = points[i][1:]
   arr[i][1] = points[i][0]
  for i in range(m):
   rank =[]
   for j in range(n):
    rank.append(points[j][i])
   rank = sorted(rank, reverse = True)
   for j in range(n):
    pos =rank.index(points[j][i])
    if(pos<arr[j][0]):
     mx[j][1] = i
     arr[j][0] = pos
   for j in range(n):
    if(points[j][i]>arr[j][1]):
     mx[j][0] = i
     arr[j][1] = points[j][i]
  ans = 0
  for x in mx:
   if(x[0]!= x[1]):
    ans+=1
  print(ans)
except EOFError as e:
 pass


for _ in range(int(input())):
 n,m=map(int,input().split())
 rating=list(map(int,input().split()))
 track=[[] for i in range(n)]
 cng=[]
 for i in range(n):
  cng.append(list(map(int,input().split())))
 for i in range(m):
  for j in range(n):
   rating[j]+=cng[j][i]
  x=sorted(enumerate(rating),key=lambda x: x[1],reverse=True)
  r=1
  if i!=0:
   if x[0][1]>track[x[0][0]][0] or r<track[x[0][0]][1]:
    if x[0][1]>track[x[0][0]][0] and r<track[x[0][0]][1]:
     track[x[0][0]][2]=False
    else:
     track[x[0][0]][2]=True
    if x[0][1]>track[x[0][0]][0]:
     track[x[0][0]][0]=x[0][1]
    if r<track[x[0][0]][1]:
     track[x[0][0]][1]=r
   for k in range(1,n):
    if x[k][1]!=x[k-1][1]:
     r=k+1
    if x[k][1]>track[x[k][0]][0] or r<track[x[k][0]][1]:
     if x[k][1]>track[x[k][0]][0] and r<track[x[k][0]][1]:
      track[x[k][0]][2]=False
     else:
      track[x[k][0]][2]=True
     if x[k][1]>track[x[k][0]][0]:
      track[x[k][0]][0]=x[k][1]
     if r<track[x[k][0]][1]:
      track[x[k][0]][1]=r
  else:
   track[x[0][0]].append(x[0][1])
   track[x[0][0]].append(1)
   track[x[0][0]].append(False)
   for k in range(1,n):
    if x[k][1]!=x[k-1][1]:
     r=k+1
    track[x[k][0]].append(x[k][1])
    track[x[k][0]].append(r)
    track[x[k][0]].append(False)
   
 out=0
 for i in track:
  if i[2]:
   out+=1
 print(out)

for _ in range(int(input())):
 n,m = map(int,input().split())
 scores = list(map(int,input().split()))
 
 usr_mnthly = {}
 usr_ranks = {}
 
 for i in range(n):
  scrs = list(map(int,input().split()))
  usr_mnthly[i] = [scores[i]]
  usr_ranks[i] = []
  for j in scrs:
   usr_mnthly[i].append(usr_mnthly[i][-1]+j)
  
 #print(usr_mnthly)
 
 for mnth in range(1,m+1):
  mnthly = {i:usr_mnthly[i][mnth] for i in range(n)}
  mnthly = sorted(mnthly.items(), key = lambda x:x[1], reverse = True)
  prev = -9999999999
  rank = 0
  pos=1
  for key,value in mnthly:
   if value == prev :
    usr_ranks[key].append(rank)
    pos+=1
   else:
    rank+=pos
    usr_ranks[key].append(rank)
    pos=1
   prev=value
 #print(usr_ranks)
    
 ans = 0
 for i in range(n):
  mnths = usr_mnthly[i][1:]
  ranks = usr_ranks[i]
  if ranks.index(min(ranks)) != mnths.index(max(mnths)):
   ans+=1
 print(ans)

# cook your dish here
for _ in range(int(input())):
 n,m=map(int,input().split())
 r=list(map(int,input().split()))
 a=[]
 for i in range(n):
  b=list(map(int,input().split()))
  count=r[i]
  for j in range(m):
   count+=b[j]
   b[j]=count
  a.append(b)
 b=[]
 p=0
 for i in range(n):
  p=[0]*m
  b.append(p)
 for i in range(m):
  c=[]
  for j in range(n):
   c.append(a[j][i])
  c.sort(reverse=True)
  for j in range(n):
   b[j][i]=c.index(a[j][i])
 count=0
 for i in range(n):
  p=a[i].index(max(a[i]))
  q=b[i].index(min(b[i]))
  if p!=q:
   count+=1
 print(count)

# cook your dish here
n=int(input())

for k in range(n):
 lst=input().split()
 N=int(lst[0])
 M=int(lst[1])

 rating=[]
 rating.append(list(map(int,input().split())))

 change=[None for i in range(N)]
 for i in range(N):
  change[i]=list(map(int,input().split()))

 for i in range(1,M+1):
  rating.append([])
 
  for j in range(0, N):
   rating[i].append(rating[i-1][j] + change[j][i-1])
 
 MaxRating = []
 MaxRatingMonth= []

 for j in range(N):
  MaxRating.append(rating[1][j])
  MaxRatingMonth.append(1)
  
  for i in range(2,M+1):
   if rating[i][j]>MaxRating[j]:
    MaxRating[j]=rating[i][j]
    MaxRatingMonth[j]=i
 MaxRankingMonth= []
 newrating=[]

 Ranking=[[None for j in range(N)] for i in range(0,M)]
 for i in range(1,M+1):
  newrating.append(sorted(rating[i],reverse=True))
 for i in range(1,M+1):
  for j in range(N):
   Ranking[i-1][j]=(newrating[i-1].index(rating[i][j])+1)
  
 MaxRankingMonth= []
 MaxRanking=[]

 for j in range(N):
  MaxRankingMonth.append(1)
  MaxRanking.append(Ranking[0][j])
  
  for i in range(2,M+1):
   if Ranking[i-1][j]<MaxRanking[j]: 
    MaxRanking[j]=Ranking[i-1][j]
    MaxRankingMonth[j]=i
   
 count=0
 for i in range(N):
  if MaxRankingMonth[i]!=MaxRatingMonth[i]:
   count+=1
 print(count)

# cook your dish here
n=int(input())

for k in range(n):
 lst=input().split()
 N=int(lst[0])
 M=int(lst[1])

 rating=[]
 rating.append(list(map(int,input().split())))

 change=[]
 for i in range(N):
  change.append([])
  change[i]=list(map(int,input().split()))

 for i in range(1,M+1):
  rating.append([])
  for j in range(0, N):
   rating[i].append(rating[i-1][j] + change[j][i-1])
 
 MaxRating = []
 MaxRatingMonth= []

 for j in range(N):
  MaxRating.append(rating[1][j])
  MaxRatingMonth.append(1)
  
  for i in range(2,M+1):
   if rating[i][j]>MaxRating[j]:
    MaxRating[j]=rating[i][j]
    MaxRatingMonth[j]=i
 Ranking = []
 MaxRankingMonth= []
 newrating=[]

 for i in range(1,M+1):
  newrating.append(sorted(rating[i],reverse=True))
  Ranking.append([])
 
  for j in range(N):
   Ranking[i-1].append(newrating[i-1].index(rating[i][j])+1)
  
 MaxRankingMonth= []
 MaxRanking=[]

 for j in range(N):
  MaxRankingMonth.append(1)
  MaxRanking.append(Ranking[0][j])
  
  for i in range(2,M+1):
   if Ranking[i-1][j]<MaxRanking[j]: 
    MaxRanking[j]=Ranking[i-1][j]
    MaxRankingMonth[j]=i
   
 count=0
 for i in range(N):
  if MaxRankingMonth[i]!=MaxRatingMonth[i]:
   count+=1
 print(count)

def getRanking(ratingList,n,m):
 rank = []
 array2d = [[None for i in range(n)] for j in range(m)]
 # print(array2d)
 for i in range(m): #months
  helper2d = [[None for i in range(2)] for j in range(n)]
  # print(helper2d)
  for j in range(n):
   helper2d[j][0] = j+1
   helper2d[j][1] = ratingList[j][i]
  # helper2d = zip(*helper2d)
  helper2d = sorted(helper2d,reverse=True,key=lambda x:x[1])
  arr = [x for x in range(1,n+1)]
  for k in range(1,n):
   if helper2d[k][1] == helper2d[k-1][1]:
    arr[k] = arr[k-1]
  # print(helper2d)
  for l in range(n):
   # print("l and i",l,i)
   array2d[i][helper2d[l][0]-1] = arr[l]
 for l in range(n):
  minimum = float('inf')
  minimumIndex = None
  for o in range(m):
   if minimum > array2d[o][l]:
    minimum = array2d[o][l]
    minimumIndex = o+1

  rank.append(minimumIndex)

  # helper2d = zip(*helper2d)
 # print(array2d)
 # print(arr)
 # print(rank)

 return rank



t = int(input())
for _ in range(t):
 n,m = list(map(int,input().split()))
 r = list(map(int,input().split()))
 c = [ None for j in range(n)]
 for i in range(n):
  c[i] = list(map(int,input().split()))
 ratingList = [None for i in range(n)]
 # print(ratingList)
 for i in range(n):
  temp = []
  temp.append(r[i]+c[i][0])
  for j in range(1,m):
   temp.append(temp[j-1]+c[i][j])
  ratingList[i] = temp
 playerRatings = []
 for i in range(n):
  maximum = float('-inf')
  maximumMonth = None
  for j in range(m):
   if ratingList[i][j] > maximum:
    maximum = ratingList[i][j]
    maximumMonth = j+1
  playerRatings.append(maximumMonth)
 playerRanking = getRanking(ratingList,n,m)
 count = 0
 for i in range(n):
  if playerRanking[i] != playerRatings[i]:
   count += 1
 print(count)
 # print(playerRatings)
 # print(playerRanking)


# cook your dish here
for _ in range(int(input())):
 n,m = list(map(int,input().split()))
 org_ratings = list(map (int,input().split()))
 l = []
 count = 0
 for i in range(n):
  l.append(list(map(int,input().split())))
 for i in range(n):
  for j in range(m):
   if(j == 0):
    l[i][j] = l[i][j] + org_ratings[i]
   else:
    l[i][j] = l[i][j] + l[i][j-1]
 #print(l)
 ranks = [[-1 for i in range(len(l[0]))] for j in range(len(l))]
 #print(ranks)
 for i in range(m):
  l2 = []
  for j in range(n):
   l2.append([l[j][i], j+1])
   
  
  l2.sort(reverse=True, key=lambda x: x[0])
  
  for ind in range(len(l2)):
   if ind == 0:
    pos = 1
   else:
    if l2[ind][0] == l2[ind-1][0]:
     pos = ranks[l2[ind-1][1]-1][i]
    else:
     pos = ind + 1
   ranks[l2[ind][1]-1][i] = pos
 #print(ranks)
 for i in range(n):
  #print(ranks[i].index(min(ranks[i])))
  #print(l[i].index(max(l[i])))
  if l[i].index(max(l[i])) != ranks[i].index(min(ranks[i])):
   count+= 1
 print(count)
   
 
 
  


import sys
from collections import defaultdict
t=int(input())

for _ in range(t):
 n,m=list(map(int,input().split()))
 a=list(map(int,input().split()))
 adj=[]
 for _ in range(n):
  x=list(map(int,input().split()))
  adj.append(x)

 rating=defaultdict(lambda:-1)


 rank=[[-1 for i in range(m+1)]for j in range(n)]


 b=[[-1 for i in range(m+1)]for j in range(n)]
 for i in range(n):
  b[i][0]=a[i]
 for i in range(0,n):
  for j in range(1,m+1):
   b[i][j]=b[i][j-1]+adj[i][j-1]

 for j in range(1,m+1):
  g=[]
  d={}
  for i in range(n):
   g.append(b[i][j])
  z=sorted(g,reverse=True) 
  c=0
  for i in z:
   if(i not in d):
    c+=1
    d[i]=c
   else:
    c+=1
  for i in range(n):
   rank[i][j]=d[b[i][j]]

 for i in range(n):
  mx=-1
  for x in range(1,m+1):
   mx=max(mx,b[i][x])
  for j in range(1,m+1):
   if(b[i][j]==mx):
    rating[i]=j
    break
 #print(rating)
 #for i in b:
  #print(*i)
 c=0
 '''for i in b:
     print(*i)
    print()
    for i in rank:
     print(*i)'''
 
 for i in range(n):
  ans=[]
  mi=sys.maxsize
  for x in range(1,m+1):
   mi=min(mi,rank[i][x])
  for j in range(1,m+1):
   if(rank[i][j]==mi):
    if(j!=rating[i]):
     c+=1
    break

 print(c)




# cook your dish here
import sys

t=int(input())
for _ in range(t):
 N,M= list(map(int,input().split()))
 C=[[0 for x in range(N)]for y in range(M+1)]
 C[0] = list(map(int,input().split()))

 rating=[0 for x in range(N)]
 rank = [sys.maxsize for x in range(N)]

 for i in range(N):
  change=list(map(int,input().split()))
  rate = 0
  month = 0
  for j in range(1,M+1):
   C[j][i]=C[j-1][i]+change[j-1]
   if C[j][i] > rate:
    rate=C[j][i]
    month=j

  rating[i]=month


 mon=[0 for x in range(N)]
 for i in range(1,M+1):
  X=sorted(C[i],reverse=True)
  for j in range(N):
   temp=X.index(C[i][j])
   if temp < rank[j] :
    rank[j]=temp
    mon[j]=i

 pl=0
 for i in range(N):
  if mon[i]==rating[i]:
   pl+=1

 print(N-pl)











def check(month_rat,month_rank,n):
 c=0
 for i in range(1,n+1):
  if(month_rat[i]!=month_rank[i]):
   c+=1
 return c
def ranking(r,m_rank,month_rank,m,n):
 for i in range(m):
  rank=1
  c=0
  for val in r[i]:
   rank+=c
   c=0
   for j in range(n):
    if(val==d[i+1,j+1]):
     if(m_rank[j+1] > rank):
      m_rank[j+1]=rank
      month_rank[j+1]=(i+1)
     c+=1
    
for _ in range(int(input())):
 n,m=list(map(int,input().split()))
 r=list(map(int,input().split()))
 c=[list(map(int,input().split())) for i in range(n)]
 rating=[]
 d={}
 m_rank={}
 m_rat={}
 month_rank={}
 month_rat={}
 f=1
 for i in range(m):
  x=[]
  for j in range(n):
   if(i==0):
    d[(i+1),(j+1)]=r[j]+c[j][i]
    x.append(d[i+1,j+1])
   else:
    d[(i+1),(j+1)]=d[i,(j+1)]+c[j][i]
    x.append(d[i+1,j+1])
   if(f):
    m_rank[j+1]=501
    m_rat[j+1]=d[i+1,j+1]
    month_rat[j+1]=(i+1)
   else:
    if(m_rat[j+1] < d[i+1,j+1]):
     m_rat[j+1]=d[i+1,j+1]
     month_rat[j+1]=(i+1)
  x=list(set(x))
  x.sort()
  rating.append(x[::-1])
  f=0
 ranking(rating,m_rank,month_rank,m,n)
 count=check(month_rat,month_rank,n)
 print(count)