Fixed Prefix Permutations
You are given $$$n$$$ permutations $$$a_1, a_2, \dots, a_n$$$, each of length $$$m$$$. Recall that a permutation of length $$$m$$$ is a sequence of $$$m$$$ distinct integers from $$$1$$$ to $$$m$$$.Let the beauty of a permutation $$$p_1, p_2, \dots, p_m$$$ be the largest $$$k$$$ such that $$$p_1 = 1, p_2 = 2, \dots, p_k = k$$$. If $$$p_1 \neq 1$$$, then the beauty is $$$0$$$.The product of two permutations $$$p \cdot q$$$ is a permutation $$$r$$$ such that $$$r_j = q_{p_j}$$$.For each $$$i$$$ from $$$1$$$ to $$$n$$$, print the largest beauty of a permutation $$$a_i \cdot a_j$$$ over all $$$j$$$ from $$$1$$$ to $$$n$$$ (possibly, $$$i = j$$$).
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 5 \cdot 10^4$$$; $$$1 \le m \le 10$$$) — the number of permutations and the length of each permutation.The $$$i$$$-th of the next $$$n$$$ lines contains a permutation $$$a_i$$$ — $$$m$$$ distinct integers from $$$1$$$ to $$$m$$$.The sum of $$$n$$$ doesn't exceed $$$5 \cdot 10^4$$$ over all testcases.
For each testcase, print $$$n$$$ integers. The $$$i$$$-th value should be equal to the largest beauty of a permutation $$$a_i \cdot a_j$$$ over all $$$j$$$ ($$$1 \le j \le n$$$).
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    T = {}
    A = []
    for _ in range(n):
        I = list(map(int, input().split()))
        J = [0] * (m + 1)
        for i in range(m):
            J[I[i]] = i + 1
        D = T
        for num in J[1:]:
            if not num in D:
                D[num] = {}
            D = D[num]
        A.append(I)
    Ans2 = []
    for data in A:
        Ans = 0
        D = T
        for num in data:
            if num in D:
                Ans += 1
                D = D[num]
            else:
                break
        Ans2.append(Ans)
    print(*Ans2)
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n1, m1) = map(int, input().split())
    T = {}
    A = []
    for _ in range(n1):
        I = list(map(int, input().split()))
        J = [0] * (m1 + 1)
        for i in range(m1):
            J[I[i]] = i + 1
        D = T
        for num in J[1:]:
            if not num in D:
                D[num] = {}
            D = D[num]
        A.append(I)
    Ans2 = []
    for data in A:
        Ans = 0
        D = T
        for num in data:
            if num in D:
                Ans += 1
                D = D[num]
            else:
                break
        Ans2.append(Ans)
    print(*Ans2)
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    t = {}
    y = []
    for _ in range(n):
        z = list(map(int, input().split()))
        q = [0] * (m + 1)
        for i in range(m):
            q[z[i]] = i + 1
        x = t
        for num in q[1:]:
            if not num in x:
                x[num] = {}
            x = x[num]
        y.append(z)
    ans2 = []
    for data in y:
        ans = 0
        x = t
        for num in data:
            if num in x:
                ans += 1
                x = x[num]
            else:
                break
        ans2.append(ans)
    print(*ans2)