Find the Mother vertex in a directed graph. A mother vertex in a graph G = (V,E) is a vertex v such that all other vertices in G can be reached by a path from v.

The first line of input contains an integer T. Then T test cases follow. Each test case contains two integers N and M. In the next line are M space separated values u,v denoting an edge from u to v.

Output: For each testcase return the mother vertex of graph (if existes) else return -1.

Note: This is a functional probem. Complete function findmother().

1 <= T <= 100
1 <= N, M <= 104
0 <= u, v < N


5 5
1 0 0 2 2 1 0 3 3 4
3 2
0 1 2 1


Testcase 1:
According to the given edges, all nodes can be reaced from nodes from 0, 1 and 2. But, since we are traversing from node 0, so 0 is the output.

Testcase 2: According to the given edges, no vertices are there from where we can reach all vertices. So, output is -1.


** For More Input/Output Examples Use 'Expected Output' option **

Author: SIDG

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.