1. 
package acm_11813_shopping;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
* FWP, Ausgew¦hlte Probleme aus dem ACM Programming Contest, SS10
* Problem: acm_11813_shopping
* Link:
*
* @author Martin Lambeck
* @version 1.0, 09.09.2010
*
* Method : dijkstra, bruteforce
* Status : Accepted
* Runtime: 2,956
*/


public class Main
{
static Scanner sc = new Scanner(System.in);

static int[][] adja;

static int best = Integer.MAX_VALUE;

public static void main(String... args)
{
int cases = sc.nextInt();

for (int i = 0; i < cases; i++)
testcase();
}

public static void testcase()
{
int n = sc.nextInt();
int m = sc.nextInt();

Node[] nodes = new Node[n];

for (int i = 0; i < n; i++)
nodes[i] = new Node(i);

for (int i = 0; i < m; i++)
{
Node n1 = nodes[sc.nextInt()];
Node n2 = nodes[sc.nextInt()];
int cost = sc.nextInt();

n1.roads.add(new Edge(n2, cost));
n2.roads.add(new Edge(n1, cost));
}

int s = sc.nextInt()+1;

Node[] shops = new Node[s];

adja = new int[s][s];

shops[0] = nodes[0];
shops[0].shop = 0;

for (int i = 1; i < s; i++)
{
shops[i] = nodes[sc.nextInt()];

if (shops[i].shop >= 0)
throw new IllegalStateException();

shops[i].shop = i;
}

for (int i = 0; i < s; i++)
{
for (int j = 0; j < n; j++)
{
nodes[j].cost = Integer.MAX_VALUE;
nodes[j].visited = false;
}

dijk(nodes, shops[i]);
}

best = Integer.MAX_VALUE;

brute(shops, 1, 0, 0);


System.out.println(best);
}



static void brute(Node[] shops, int lv, int c, int p)
{
int nc;

if (lv == shops.length)
{
if (c + adja[p][0] < best)
best = c + adja[p][0];

} else
{
for (int i = 1; i < shops.length; i++)
{
if (!shops[i].used)
{
shops[i].used = true;

nc = c + adja[p][i];

if (nc < best)
brute(shops, lv+1, nc, i);

shops[i].used = false;
}
}
}
}

static void dijk(Node[] nodes, Node start)
{
PriorityQueue<Node> pq = new PriorityQueue<Node>(nodes.length);

start.cost = 0;

pq.add(start);

while (pq.size() > 0)
{
Node nd = pq.poll();

if (nd.visited)
continue;

nd.visited = true;

if ((start.shop >= 0) && (nd.shop >= 0))
adja[start.shop][nd.shop] = nd.cost;

for (Edge e : nd.roads)
{
if (!e.to.visited && (nd.cost + e.cost < e.to.cost))
{
e.to.cost = nd.cost + e.cost;
pq.add(e.to);
}
}
}
}
}

class Node implements Comparable<Node>
{
boolean visited = false;
int cost = Integer.MAX_VALUE;
int id;
int shop = -1;
LinkedList<Edge> roads = new LinkedList<Edge>();

boolean used = false;

public Node(int d)
{
id = d;
}

public int compareTo(Node o)
{
if (cost == o.cost)
return 0;

return (cost < o.cost ? -1 : 1);
}
}

class Edge
{
Node to;
int cost;

public Edge (Node t, int c)
{
to = t;
cost = c;
}
}