SP349 AROUND - Around the world

Description

Over the years, FJ has made a huge number of farmer friends all around the world. Since he hasn't visited 'Farmer Ted' from England and 'Boer Harms' from Holland for a while, he'd like to visit them. He knows the longitude of the farm where each of his worldwide friends resides. This longitude is an angle (an integer in the range 0..359) describing the farm's location on the Earth, which we will consider to be a circle instead of the more complex and traditional spherical representation. Except for the obvious discontinuity, longitudes increase when traveling clockwise on this circle. FJ plans to travel by airplane to visit his N (1

Input Format

The first line of the input contains an integer t representing the number of test cases. Then t test cases follow. Each test case has the following form: - Line 1: Two space-separated integers: N and M - Lines 2..N+1: Line i+1 contains one integer: the longitude of the i-th farm. Line 2 contains the location of the farm of his best friend. - Lines N+2..N+M+1: Line i+N+1 contains two integers giving the indices of two farms that are connected by a flight.

Output Format

For each test case, output a single integer specifying the minimum number of flights FJ needs to visit to make a trip around the world. Every time FJ moves from one farm to another counts as one flight. If it is impossible to make such a trip, output the integer -1.

Explanation/Hint

Input details Farmer John has three friends at longitudes 0, 120, and 240. There are three flights: 0120, 120240, and 0240. The journey must start and finish at longitude 0.