#P14197. [ICPC 2024 Hangzhou R] Kind of Bingo

[ICPC 2024 Hangzhou R] Kind of Bingo

题目描述

There is a grid with nn rows and mm columns. The cells in the grid are numbered from 11 to n×mn \times m, where the cell on the ii-th row and the jj-th column is numbered as ((i1)×m+j)((i - 1) \times m + j).

Given a permutation p1,p2,,pn×mp_1, p_2, \cdots, p_{n \times m} of n×mn \times m, we're going to perform n×mn \times m operations according to the permutation. For the ii-th operation, we'll mark cell pip_i. If after the bb-th operation, there is at least one row such that all the cells in that row are marked, and bb is as small as possible, then we say bb is the bingo integer of the permutation.

You're given the chance to modify the permutation at most kk times (including zero times). Each time you can swap a pair of elements in the permutation. Calculate the smallest possible bingo integer after the modifications.

Recall that a sequence p1,p2,,pn×mp_1, p_2, \cdots, p_{n \times m} of length n×mn \times m is a permutation of n×mn \times m if and only if each integer from 11 to n×mn \times m (both inclusive) appears exactly once in the sequence.

输入格式

There are multiple test cases. The first line of the input contains an integer TT (1T1041 \le T \le 10^4) indicating the number of test cases. For each test case:

The first line contains three integers nn, mm and kk (1n,m1051 \le n, m \le 10^5, 1n×m1051 \le n \times m \le 10^5, 0k1090 \le k \le 10^9), indicating the number of rows and columns of the grid and the number of modifications you can perform.

The second line contains n×mn \times m distinct integers p1,p2,,pn×mp_1, p_2, \cdots, p_{n \times m} (1pin×m1 \le p_i \le n \times m).

It's guaranteed that the sum of n×mn \times m of all test cases will not exceed 10510^5.

输出格式

For each test case output one line containing one integer indicating the smallest possible bingo integer after the modifications.

3
3 5 2
1 4 13 6 8 11 14 2 7 10 3 15 9 5 12
2 3 0
1 6 4 3 5 2
2 3 1000000000
1 2 3 4 5 6
7
5
3

提示

For the first sample test case, we can first swap 11 and 1515, then swap 66 and 1212 to get the sequence $[15, 4, 13, 12, 8, 11, 14, 2, 7, 10, 3, 1, 9, 5, 6]$. It's easy to see that after the 77-th operation, all cells in the 33-rd row will be marked.

For the second sample test case, it's easy to see that after the 55-th operation, all cells in the 22-nd row will be marked.

For the third sample test case, we don't need to make any modifications. It's easy to see that after the 33-rd operation, all cells in the 11-st row will be marked.