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.

**Input:**

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().**

**Constraints:**

1 <= T <= 100

1 <= N, M <= 10^{4}

0 <= u, v < N

**Example:
Input:**

2

5 5

1 0 0 2 2 1 0 3 3 4

3 2

0 1 2 1

**Ouput:**

0

-1

**Explanation:
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.

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.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

madhursengar24 | 86 |

PiyushPandey4 | 617 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 448 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.