Vacation Plans

Find the minimum total cost for up to three people to each travel from city 1 to their airports across separate countries in the same number of days.

Hard8Shortest pathDynamic programmingGraphMathNo attempts yetTime limit1sMemory limit512 MB

Problem

A group of pp people plans a winter vacation on a remote island near the equator. Every member lives in a different country, and the island is reachable only by airplane. Each country has exactly one airport, so every member has to reach the airport city of their own country. The group agreed that everyone is at their airport on the same day, and that day does not have to be the first day of the trip.

Each country has nn cities and mm one way roads. City 1 is the home city, and city aa holds the airport. On day 0 every member is in city 1 of their own country. On each day after that, a member either travels along one road out of the current city, or stays in the current city. Travelling from city uu to city vv along a road costs gg, and staying in city vv costs the cheapest hotel price hvh_v of that city. Every member picks exactly one of the two options every day.

The members pool their money, so they plan as a group. After the same number of days DD, every member has to be in the airport city of their own country. DD can be 0. Find the smallest possible total cost.

Roads are directed, and the cost from uu to vv does not have to equal the cost from vv to uu. No road connects a city to itself. In every country at least one route runs from city 1 to the airport city.

Figure L.1. The city layouts of two members. The airports are in city 4 and city 3, and the home city is 1 for both. gg is the cost of travelling along a road, and hh is the cheapest hotel price.

In Figure L.1 the cheapest plan works like this. On day 1 the first member moves to city 3 and the second member moves to city 2. On day 2 the first member moves to city 4 and the second member moves back to city 1. On day 3 the first member stays in city 4 and the second member moves to city 3. The cost is (1+5+1)+(3+2+4)=16(1 + 5 + 1) + (3 + 2 + 4) = 16.

Input

The first line contains the integer pp, the number of members in the group and therefore also the number of countries (1p31 \le p \le 3).

The descriptions of the pp countries follow. The first line of a country description contains two integers nn and mm, the number of cities and the number of roads (1n501 \le n \le 50, n1m4nn - 1 \le m \le 4n). The next nn lines contain one integer each, the cheapest hotel price of city 1 through city nn in that order (0h1,000,0000 \le h \le 1{,}000{,}000). The next mm lines each contain three integers uu, vv and gg, meaning that a one way road runs from city uu to city vv at cost gg (1u,vn1 \le u, v \le n, uvu \ne v, 0g1,000,0000 \le g \le 1{,}000{,}000). The last line of a country description contains the integer aa, the city that holds the airport (1an1 \le a \le n). City 1 is the home city in every country.

Output

Print one line with a single integer, the smallest total cost that puts every member in the airport city of their own country after the same number of days.