CF1792D Fixed Prefix Permutations

Description

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 $ ).

Input Format

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.

Output Format

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 $ ).