There is a villa which has n rooms placed one after the other , numbered from 1 to N. There are two types of rooms - normal rooms and exit rooms. Rahul starts from room number 1 (only) to next room (given in the array), and so on . If it is a normal room he will keep on moving to other rooms but if he finds an exit room then he would reach out of the villa. Your task is to help Rahul in finding the last room which he can reach.

**Input:**

The first line contains a integer T i.e. the number of test cases.The first line of each test case contains three integers N, M and K — the number of doors, the number of exit doors in the room, and the number of operations, respectively. The second line of each test case contains M distinct integers h1, h2 , . . . , hm — the positions where there is a exit door. The third line of each test case contains K paired integers — which shows his movement from room Ai.first to room Ai.second or Ai.second room to room Ai.first.

**Output:**

For each test case in a new line print one integer denoting the room number outside which Rahul is standing.

**Constraints:**

1 <=T <=100

2 <= N <= 1000

1 <= M <= N

1 <= K <= 1000

**Examples:
Input:**

2

5 1 2

2

1 2 2 4

7 3 4

3 4 6

1 2 2 5 5 7 7 1

2

1

**Explanation:**

Example 1: There is only one exit room i.e. at room2. So at beginning Rahul is at room1 as always. As he performs 1st movement, he would reach at room2. And now any other movement will not affect the position of Rahul. Hence Final position Room2.

Author: saksham0751

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.

ramabhatta | 35 |

Shubham Awasthi | 34 |

pguptacse17 | 30 |

abhithakur588 | 28 |

adityasuman2025 | 27 |

the_coder95 | 1236 |

RishabhTanwar1 | 1104 |

thanuvinu94 | 676 |

tathagat289 | 664 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4889 |

Quandray | 4547 |

Login to report an issue on this page.