Update appNew update is available. Click here to update.

Find the Town Judge

Posted: 27 Mar, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

In a town, there are ‘N’ people, each person number from [1, 'N']. We know that one of these people is a secret judge. There are the following facts related to the judge:

1. The judge trust nobody
2. Everybody trusts the judge
3. There is only one judge.

You are given an array/list of pairs, representing that the first value of the pair trusts the second value of the pair. You need to find the town judge. If there is no possible judge return -1.

Note:
Note that the judge is also numbered similar to the rest of the town members.
Input Format:
The first line contains an integer ‘T’ which denotes the number of test cases. 

The first line of each test case contains two space-separated integers ‘N’ and ‘X’, denoting the number of people in the town and the number of queries regarding who trusts whom.

The next ‘X’ lines of each test contain an array of ‘X’ pairs where each pair denotes who trusts whom in the town.
Output Format:
For each test case, you need to return a single integer denoting the judge.

Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 1000
0 <= X <= 1000
1 <= Y1, Y2 <= N

Where 'Y1' and 'Y2' denotes who trusts whom in the town.

Time limit: 1 sec