University Rankings

Given M rankings of N universities, find the longest sequence where each earlier university beats the next in every ranking.

Medium6Dynamic programmingSortingPrefix sumNo attempts yetTime limit8sMemory limit512 MB

Problem

A rating agency publishes one ranking list per department. Every university in the survey runs the same MM departments, and each department list places all NN universities in a strict order from best to worst.

Because there are many lists, two universities often cannot be ordered against each other. The agency therefore uses a relation it calls absolutely better: university XX is absolutely better than university YY when XX comes before YY in every one of the MM department rankings.

Take three universities XX, YY, ZZ and three departments CS, EE, FLS with these rankings:

  • CS: XX, YY, ZZ
  • EE: XX, ZZ, YY
  • FLS: ZZ, XX, YY

XX comes before YY in all three lists, so XX is absolutely better than YY. XX and ZZ cannot be ordered: XX leads in CS, ZZ leads in FLS.

Find universities U1,,UkU_1, \dots, U_k such that UiU_i is absolutely better than UjU_j for every i<ji < j. Report the largest possible kk. Universities are given as numbers.

Input

The first line holds one integer CC, the number of test cases (1C101 \le C \le 10).

Each test case begins with a line holding two integers NN and MM (1N,M4001 \le N, M \le 400), the number of universities and the number of departments. MM lines follow. The kk-th of them holds NN integers, a permutation of 11 through NN, listing the universities of the kk-th department's ranking from best to worst. If university UU appears before university VV on that line, the kk-th department of UU is better than the kk-th department of VV.

Output

Print CC lines, one per test case in the order given, each holding the largest possible kk. Print no extra spaces.