#K74792. Kth Tallest Building

    ID: 34276 Type: Default 1000ms 256MiB

Kth Tallest Building

Kth Tallest Building

You are given a grid representing a city where each cell contains the height of a building. A value of 0 indicates that there is no building at that cell. For each test case, your task is to determine the height of the k-th tallest building. If there are fewer than k buildings in the grid, you should output -1.

Formally, let \(H = [h_1, h_2, \dots, h_n]\) be the list of all building heights (where each \(h_i > 0\)). After sorting \(H\) in descending order, the answer is given by:

[ \text{Answer} = \begin{cases} H[k-1], & \text{if } |H| \ge k \ -1, & \text{otherwise} \end{cases} ]

Implement the solution so that it reads from standard input (stdin) and writes to standard output (stdout).

inputFormat

The input starts with an integer T representing the number of test cases. For each test case, the first line contains two integers n and m which denote the number of rows and columns in the grid respectively. The following n lines each contain m integers, representing the grid values. The next line contains an integer k, indicating that you need to find the k-th tallest building in the grid.

T
n m
row1
row2
... (n rows)
k

outputFormat

For each test case, output a single line containing the height of the k-th tallest building. If there are fewer than k buildings, output -1.

## sample
2
3 3
1 2 3
4 0 6
7 8 0
3
2 2
0 0
0 0
1
6

-1

</p>