Given a weighted undirected graph and K friends' rooms, pick the room minimizing the sum of shortest-path distances from all friends; break ties by smallest room.
Medium4Shortest pathGraphBrute forceInterviewNo attempts yetTime limit1sMemory limit128 MBHarry and his friends want to hold a secret meeting to practice Defence Against the Dark Arts away from Umbridge's watch. They pass the location with fake Galleons. When Harry writes a place on his own Galleon, the same place appears on the Galleons his friends carry.
Hogwarts has N rooms that suit the meeting. Each room carries one number from 1 to N, and no number repeats. The rooms are joined by M secret passages built with magic. Every passage can be walked in both directions and its length is a natural number. K friends attend the meeting.
Harry picks one of the N rooms as today's meeting place. Before he picks it he checks the Hogwarts secret map and finds the friends standing in different rooms, one friend per room. Apparition is banned inside Hogwarts, so each friend walks to the meeting place through secret passages only, and always along a shortest route from the room where they started. The travel distance of a friend is the sum of the lengths of the passages on that route.
Harry uses the room that makes the total travel distance of the attending friends as small as possible. The picture below shows an example with N=6, M=7, K=2.

The number in each vertex is a room number, and the number on each edge is the length of the passage between those two rooms. The two friends are in room 3 and room 5. If room 2 becomes the meeting place, the friend in room 3 walks 3, 2 for a travel distance of 2, and the friend in room 5 walks 5, 1, 3, 2 for a travel distance of 5, so the total is 7. If room 1 becomes the meeting place, the two travel distances are 1 and 2 and the total is 3. In this example rooms 1, 3 and 5 all reach the smallest total, 3.
Once Harry writes the meeting place on the fake Galleon, the K friends learn it at once, stop what they are doing and leave for that room. Write a program that finds the meeting place minimizing the total travel distance of the friends.
Input is read from standard input. The first line holds the number of test cases T.
The first line of each test case holds the number of rooms N and the number of secret passages M, separated by a space (2≤N≤100, N−1≤M≤N(N−1)/2). Each of the next M lines holds one passage as a, b, c. Here a and b are the numbers of the two rooms the passage joins, and c is its length. a and b are always different, and c is a natural number between 1 and 1000. At most one passage joins any pair of rooms, and no passage is listed twice. Every room can be reached from every other room through passages.
The next line holds the number of attending friends K (1≤K≤N). The last line of each test case holds the K room numbers where the friends stand, separated by spaces. Those numbers lie between 1 and N and are all different, so two friends are never in the same room.
Output is written to standard output. For each test case print, on a line of its own and in the order the test cases are given, the number of the room that minimizes the total travel distance of the friends. If several rooms reach that minimum, print the smallest such room number.