This is an easier version of the next problem. The difference is only in constraints.

You are given a rectangular $n \times m$ matrix $a$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times.

After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $i$-th row it is equal $r_i$. What is the maximal possible value of $r_1+r_2+\ldots+r_n$?


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 40$), the number of test cases in the input.

The first line of each test case contains integers $n$ and $m$ ($1 \le n \le 4$, $1 \le m \le 100$) — the number of rows and the number of columns in the given matrix $a$. 

Each of the following $n$ lines contains $m$ integers, the elements of $a$ ($1 \le a_{i, j} \le 10^5$).


-----Output-----

Print $t$ integers: answers for all test cases in the order they are given in the input.


-----Example-----
Input
2
2 3
2 5 7
4 2 4
3 6
4 1 5 2 10 4
8 6 6 4 9 10
5 4 9 5 8 7

Output
12
29



-----Note-----

In the first test case, you can shift the third column down by one, this way there will be $r_1 = 5$ and $r_2 = 7$.

In the second case you can don't rotate anything at all, this way there will be $r_1 = r_2 = 10$ and $r_3 = 9$.

Difficulty: competition

Starter code:

Solutions:
rnd_mod = 1234567890133
rnd_x = 987654321098
def rnd():
    nonlocal rnd_x
    rnd_x = rnd_x**2 % rnd_mod
    return (rnd_x>>5) % (1<<20)
def randrange(a):
    return rnd() % a

T = int(input())
for _ in range(T):
    N, M = list(map(int, input().split()))
    X = []
    for __ in range(N):
        X.append([int(a) for a in input().split()])
    Y = [[X[i][j] for i in range(N)] for j in range(M)]
    ma = 0
    for t in range(577):
        for i in range(M):
            a = randrange(N)
            Y[i] = [Y[i][j-a] for j in range(N)]
        ma = max(ma, sum([max([Y[i][j] for i in range(M)]) for j in range(N)]))
    print(ma)


import sys
input = sys.stdin.readline

t=int(input())
for testcases in range(t):
    n,m=list(map(int,input().split()))
    A=[list(map(int,input().split())) for i in range(n)]

    B=[]
    for j in range(m):
        B.append([A[i][j] for i in range(n)])

    B.sort(key=lambda x:max(x),reverse=True)

    B=B[:n]

    #print(B)
    LEN=len(B)

    if LEN==1:
        print(sum(B[0]))

    elif LEN==2:
        ANS=0
        for i in range(n):
            A=0
            for k in range(n):
                A+=max(B[0][k],B[1][(i+k)%n])

            ANS=max(ANS,A)

        print(ANS)

    elif LEN==3:

        ANS=0
        for i in range(n):
            for j in range(n):
                    
                A=0
                for k in range(n):
                    A+=max(B[0][k],B[1][(i+k)%n],B[2][(j+k)%n])

                ANS=max(ANS,A)

        print(ANS)

    elif LEN==4:

        ANS=0
        for i in range(n):
            for j in range(n):
                for l in range(n):
                    
                    A=0
                    for k in range(n):
                        A+=max(B[0][k],B[1][(i+k)%n],B[2][(j+k)%n],B[3][(l+k)%n])

                    ANS=max(ANS,A)

        print(ANS)
        
            

    
            


from sys import stdin

def f(lst, num):
    new = lst[num:] + lst[:num]
    return new

t = int(stdin.readline())
for i in range(t):
    row, col = tuple(int(x) for x in stdin.readline().split())
    lst = list([int(x)] for x in stdin.readline().split())
    
    for j in range(row-1):
        line = tuple(int(x) for x in stdin.readline().split())
        for k in range(len(line)):
            lst[k].append(line[k])
        
    lst.sort(key=lambda x: max(x), reverse = True)

    ans = float('-inf')
    for a in range(4):
        for b in range(4):
            for c in range(4):
                for d in range(4):
                    if col >= 1:
                        aa = f(lst[0], a)
                    else:
                        aa = (0,)*row
                    if col >= 2:
                        bb = f(lst[1], b)
                    else:
                        bb = (0,)*row
                    if col >= 3:
                        cc = f(lst[2], c)
                    else:
                        cc = (0,)*row
                    if col >= 4:
                        dd = f(lst[3], d)
                    else:
                        dd = (0,)*row

                    ans = max(ans,
                              sum(max(x[j] for x in (aa, bb, cc, dd))
                                  for j in range(row)))
    print(ans)


from random import randint
for _ in range(int(input())):
    n, m = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(n)]
    ans = 0
    for _ in range(100):
        for j in range(m):
            x = randint(0, n - 1)
            if x:
                B = []
                for i in range(n):
                    B.append(A[i][j])
                B = B[x:] + B[:x]
                for i in range(n):
                    A[i][j] = B[i]
        c = 0
        for i in range(n):
            c += max(A[i])
        ans = max(ans, c)
    print(ans)

t = int(input())
for i in range(t):
    n, m = [int(item) for item in input().split()]
    mat = []
    col = [[] for _ in range(m)]
    for j in range(n):
        line = [int(item) for item in input().split()]
        for k, item in enumerate(line):
            col[k].append(item)
        mat.append(line)
    colmax = []
    for line in col:
        colmax.append([max(line), line])
    colmax.sort(reverse=True)
    colmax = colmax[:n]
    ans = 0
    for j in range(n ** (n-1)):
        index = j
        rot = [0]
        for k in range(n-1):
            rot.append(index % n)
            index //= n 
        ret = 0
        for l in range(n):
            val = 0
            for k in range(len(colmax)):
                val = max(val, colmax[k][1][(l + rot[k]) % n])
            ret += val
        ans = max(ans, ret)
    print(ans)

import random 
for _ in range(int(input())):
    N, M = list(map(int, input().split()))
    X = [[int(a) for a in input().split()] for _ in range(N)]
    Y = [[X[i][j] for i in range(N)] for j in range(M)]
    ma = 0
    for t in range(99):
        for i in range(M):
            a = random.randrange(N)
            Y[i] = [Y[i][j-a] for j in range(N)]
        ma = max(ma, sum([max([Y[i][j] for i in range(M)]) for j in range(N)]))
    print(ma)


q = int(input())
for rquer in range(q):
	c, r = list(map(int, input().split()))
	matt = [list(map(int,input().split())) for i in range(c)]
	mat = [[matt[i][j] for i in range(c)] for j in range(r)]
	for i in range(r):
		mat[i].append(max(mat[i]))
		mat[i].reverse()
	mat.sort()
	mat.reverse()
	work = mat[:min(4, r)]
	for t in work:
		t.pop(0)
	r = min(4, r)
	wyn = 0
	for num in range(c**r):
		shif = [(num//(c**i))%c for i in range(r)]
		new = 0
		for i in range(c):
			kol = [work[j][(i + shif[j])%c] for j in range(r)]
			new += max(kol)
		wyn = max(wyn, new)
	print(wyn)


for _ in range(int(input())):
    N, M = map(int, input().split())
    X = [[int(a) for a in input().split()] for _ in range(N)]
    Y = [[X[i][j] for i in range(N)] for j in range(M)]
    ma = 0
    dp = [[0] * (1<<N) for _ in range(M+1)]
    for j in range(M):
        for mask in range(1<<N):
            maskpre = mask
            while maskpre >= 0:
                maskpre &= mask
                ma = 0
                for k in range(N):
                    s = 0
                    for i in range(N):
                        if (maskpre >> i) & 1 == 0 and (mask >> i) & 1:
                            s += X[i-k][j]
                    ma = max(ma, s)
                dp[j+1][mask] = max(dp[j+1][mask], dp[j][maskpre] + ma)
                
                maskpre -= 1
    print(dp[-1][-1])

t = int(input())
def maxsa(A):
    ans = 0
    #print("asdasd")
    for i in range(n):
        cur_maxx = 0
        for j in range(4):
            cur_maxx = max(cur_maxx, A[j][i])
        ans+= cur_maxx
    return ans

def fu(A):
    answer = 0
    for j in range(n):
        A[0] = A[0][1:] + A[0][:1]
        for i in range(n):
            A[1] = A[1][1:] + A[1][:1]
            for k in range(n):
                A[2] = A[2][1:] + A[2][:1]
                for l in range(n):
                    A[3] = A[3][1:] + A[3][:1]
                    #print(A)
                    cur_ans = maxsa(A)
                    answer = max(answer, cur_ans)
    return answer


for j in range(t):
    n,m = map(int,input().split())
    A = [0] * n
    inds = [-1,-1,-1,-1]
    maxs  =[ 0,0,0,0]
    for j in range(n):
        A[j] = list(map(int,input().split()))
    for j in range(m):
        cur_maxs = 0
        for i in range(n):
            cur_maxs = max(cur_maxs, A[i][j])
        maxs.append(cur_maxs)
        inds.append(j)
        ind  = 4
        #print(cur_maxs)
        while ind !=0 and maxs[ind] > maxs[ind-1]:
            inds[ind], inds[ind-1] = inds[ind-1] , inds[ind]
            maxs[ind], maxs[ind - 1] = maxs[ind - 1], maxs[ind]
            ind-=1
        maxs.pop()
        inds.pop()


    #print(maxs)
    #print(inds)
    S = [0] * 4
    for j in range(4):
        if inds[j] != -1:
            #print(A)
            #print(inds[j])
            S[j] = [s[inds[j]] for s in A]
            #print(S[j])
        else:
            S[j] = [0] * n
    #print(S)
    print(fu(S))

t = int(input())
def maxsa(A):
    ans = 0
    #print("asdasd")
    for i in range(n):
        cur_maxx = 0
        for j in range(4):
            cur_maxx = max(cur_maxx, A[j][i])
        ans+= cur_maxx
    return ans

def fu(A):
    answer = 0
    for j in range(n):
        A[0] = A[0][1:] + A[0][:1]
        for i in range(n):
            A[1] = A[1][1:] + A[1][:1]
            for k in range(n):
                A[2] = A[2][1:] + A[2][:1]
                for l in range(n):
                    A[3] = A[3][1:] + A[3][:1]
                    #print(A)
                    cur_ans = maxsa(A)
                    answer = max(answer, cur_ans)
    return answer


for j in range(t):
    n,m = map(int,input().split())
    A = [0] * n
    inds = [-1,-1,-1,-1]
    maxs  =[ 0,0,0,0]
    for j in range(n):
        A[j] = list(map(int,input().split()))
    for j in range(m):
        cur_maxs = 0
        for i in range(n):
            cur_maxs = max(cur_maxs, A[i][j])
        maxs.append(cur_maxs)
        inds.append(j)
        ind  = 4
        #print(cur_maxs)
        while ind !=0 and maxs[ind] > maxs[ind-1]:
            inds[ind], inds[ind-1] = inds[ind-1] , inds[ind]
            maxs[ind], maxs[ind - 1] = maxs[ind - 1], maxs[ind]
            ind-=1
        maxs.pop()
        inds.pop()


    #print(maxs)
    #print(inds)
    S = [0] * 4
    for j in range(4):
        if inds[j] != -1:
            #print(A)
            #print(inds[j])
            S[j] = [s[inds[j]] for s in A]
            #print(S[j])
        else:
            S[j] = [0] * n
    #print(S)
    print(fu(S))

from random import randint
for _ in range(int(input())):
    n, m = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(n)]
    ans = 0
    for _ in range(100):
        for j in range(m):
            x = randint(0, n - 1)
            if x:
                B = []
                for i in range(n):
                    B.append(A[i][j])
                B = B[x:] + B[:x]
                for i in range(n):
                    A[i][j] = B[i]
        c = 0
        for i in range(n):
            c += max(A[i])
        ans = max(ans, c)
    print(ans)




def solve(matrix, col, N, M):
    if col == M:
        '''
        for row in matrix:
            print(row)
        print()
        '''

        ans = 0
        for row in matrix:
            if len(row) == 1:
                ans += row[0]
            else:
                ans += max(*row)

        return ans

    # girar la columna `col` N - 1 veces

    if N == 1:
        return solve(matrix, col + 1, N, M)

    ans = solve(matrix, col + 1, N, M)
    for _ in range(N-1):
        tmp = matrix[0][col]
        for n in range(1, N):
            matrix[n-1][col] = matrix[n][col]
        matrix[N-1][col] = tmp

        local_ans = solve(matrix, col + 1, N, M)
        if local_ans > ans:
            ans = local_ans

    return ans

def main():
    T = int(input())
    for t in range(T):
        N, M = list([int(x) for x in input().split()])

        matrix = []
        for n in range(N):
            matrix.append(
                list([int(x) for x in input().split()])
            )

        elements = []
        for n in range(N):
            for m in range(M):
                elements.append((matrix[n][m], m))

        elements.sort(reverse=True)

        candidates = []
        for t in elements:
            if t[1] not in candidates:
                candidates.append(t[1])
                if len(candidates) == N:
                    break

        simplified = []
        for n in range(N):
            row = []
            for m in candidates:
                row.append(matrix[n][m])
            simplified.append(row)

        ans = solve(simplified, 0, N, min(N, M))
        print(ans)

main()


for _ in range(int(input())):
    N, M = list(map(int, input().split()))
    X = [[int(a) for a in input().split()] for _ in range(N)]
    Y = [[X[i][j] for i in range(N)] for j in range(M)]
    ma = 0
    dp = [[0] * (1<<N) for _ in range(M+1)]
    for j in range(M):
        for mask in range(1<<N):
            maskpre = mask
            while maskpre >= 0:
                maskpre &= mask
                ma = 0
                for k in range(N):
                    s = 0
                    for i in range(N):
                        if (maskpre >> i) & 1 == 0 and (mask >> i) & 1:
                            s += X[i-k][j]
                    ma = max(ma, s)
                dp[j+1][mask] = max(dp[j+1][mask], dp[j][maskpre] + ma)
                
                maskpre -= 1
    print(dp[-1][-1])