


The first line of input contains a single integer T, denoting the number of test cases.
The first line of each test case contains ‘N’, denoting the number of cities, ‘M’ denoting the number of trains, and ‘K’ denoting the size of the connected network of cities.
The second line of each test case contains 'N' space-separated integers denoting the value associated with the i-th city.
The next 'M' lines of each test case contains ‘M’ pairs (u, v), denoting a train available from city u to city v.
The first and only line of each test case contains the maximum threshold value X, if the maximum threshold value is infinite the return 10 ^ 9. If there is no connected network of cities of size at least K, then return -1.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= k <= N <= 10^5
1 <= M <= 10^5
1 <= value of nodes <= 10 ^ 9
Time limit: 2 second
We can select a value of k from 10^9 to 1 using Binary search, and then using this k value, and we can check that if nodes with values less than K are removed, we see how many connected components are left. If the number of related components is greater than X, it means that this value is suitable for K. If the number of connected components is less than K, then we have decreased the value of K. If at some point while decreasing K by using Binary search if for a particular value of K the number of connected components is more than X then we can ignore the left part and do a binary search on the right side only. To find the number of related components in a graph, we are using Kosaraju’s algorithm, where we do DFS on the nodes and find a list of all those nodes which can be reached from all other nodes. A graph is strongly connected if there is a path between all pairs of vertices.
The steps are as follows: