Minimum cost

3

43 votes
Algorithms, Breadth First Search, Easy, Graphs
Problem

You are standing at position . From position , you can walk to or with cost . From position , you can travel to without any cost to ( is a permutation of numbers ). You have to reach position . Determine the minimum possible cost.

Input format

  • First line:  denotes the number of test cases ()
  • For each test case:
    • First line:  ()
    • Second line: integers where the  integer is
      Note: It is guaranteed that is a permutation.

Output format
Print the minimum possible cost.

Sample Input
6
6
1 4 3 2 5 6
6
3 2 6 5 4 1
6
3 6 2 4 1 5
6
5 4 6 3 2 1
6
1 6 5 4 2 3
6
5 1 3 6 2 4
Sample Output
3
0
0
0
1
1
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

-

Editor Image

?