Rahul and his Villa
Submissions: 537   Accuracy:

14%

  Difficulty: Easy   Marks: 2

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
Output:
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.

 

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

Contributor: Saksham Ashtputre
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.



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


to report an issue on this page.