By Paulo Oliva * Brazil
The mayor of a city wants to introduce a new transport system to simplify the life of its inhabitants. That will be done via the use of a debit card, which the mayor named “GoEasy”. There are two means of transportation in the city: trains and buses. The train system is “zone based” whereas the bus system is “journey based”. The fare for a journey is computed as follows:
A transport system map will provide information about the stations belonging to each zone, and the sequence of stations for each bus and train itinerary. Buses and trains move in both directions in each itinerary, and no train or bus goes through the same station twice during a single trip through an itinerary. It is always possible to go from any station to any other station using trains and/or buses. The rules for computing fares are strict: if during a train journey a customer enters a given zone twice, she/he is charged twice; similarly, if during a bus journey a customer boards twice the bus for the same itinerary, she/he is charged twice.
In the transport map above a customer can travel from station 2 to station 4 paying just two money units, by using line T1, since they are in the same zone. But if the customer needs to go from station 2 to 5, then the best is to take the bus B3 to station 10 and then take the bus B2 to station 5, paying in total four money units. Rather than tracking the whole trip of each passenger, the idea of the mayor is that machines will be placed in all stations, and travellers are supposed to swipe their personal GoEasy travel card only when starting AND finishing the whole journey. Since all the machines are interconnected into a network, based on the departure and arrival stations the system can compute the minimum cost possible for the trip, and that amount is charged from the traveller’s debit card. All that is missing is a computer system for doing the calculations for the fare to be deducted. So, given the map of the transport system in the city, you must write a program to compute the minimum fare the customer should pay to travel between two given stops/stations.
The input contains several test cases. The first line of a test case contains two integers Z and S, which indicate respectively the number of zones (1 ≤ Z ≤ 30) and the number of train/bus stations in the city (1 ≤ S ≤ 100). Each station has a unique identification number ranging from 1 to S, and each station belongs to exactly one zone. Each of the following Z lines describes the stations belonging to a zone. The description for a zone starts with an integer K indicating the number of stations (1 ≤ K ≤ S) in the zone, followed by K integers representing the stations in the zone. After that comes a line with two integer numbers T and B, representing respectively the number of train itineraries (1 ≤ T ≤ 50) and the number of bus itineraries (1 ≤ B ≤ 50). Next comes T lines describing train itineraries, followed by B lines describing bus itineraries. The description of each itinerary consists of a line containing an integer L indicating the number of stations (2 ≤ L ≤ S) in the itinerary, followed by L integers specifying the sequence of stations in the itinerary. Finally it comes a line with two integers X and Y (1 ≤ X ≤ S, 1 ≤ Y ≤ S and X ≠ Y ), specifying that the customer travelled from station X to station Y . The end of input is indicated by Z = S = 0.
For each test case your program should output one line, containing an integer representing the amount to be deducted from the traveller’s GoEasy card.
|Sample Input||Sample Output|