package com.company.real;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
public class ZuiduanLujingShu_20200731 {
static NodePro0731[] nodes;
static long lencount;
static int pid[];
static int shortWeight[];
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("C:\\Users\\XAGDC\\Desktop\\sw\\PRO\\58Pro 最短路径生成树 The Shortest Path Spanning Tree\\eval_input.txt"));
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(reader.readLine());
int T=Integer.parseInt(st.nextToken());
for (int zz = 0; zz <T ; zz++) {
long startTime=System.currentTimeMillis();
st=new StringTokenizer(reader.readLine());
int N=Integer.parseInt(st.nextToken());//N个顶点
int M=Integer.parseInt(st.nextToken());//M条边
int S=Integer.parseInt(st.nextToken());//S是起点
nodes=new NodePro0731[N];
shortWeight=new int[N];
pid=new int[N];
for (int i = 0; i <nodes.length ; i++) {
nodes[i]=new NodePro0731();
}
for (int i = 0; i <M ; i++) {
st=new StringTokenizer(reader.readLine());
int start=Integer.parseInt(st.nextToken());
int end=Integer.parseInt(st.nextToken());
int weight=Integer.parseInt(st.nextToken());
nodes[start].sonList.add(new int[]{end,weight});
nodes[end].sonList.add(new int[]{start,weight});
}
process(S);
lencount=0;
for (int i = 0; i <nodes.length ; i++) {
lencount=lencount+shortWeight[i];
}
//System.out.printf("pid: %s \n", Arrays.toString(pid));
//System.out.printf("shortWeight: %s \n", Arrays.toString(shortWeight));
System.out.printf("#%d %d\n",(zz+1),lencount);
//System.out.println(System.currentTimeMillis()-startTime);
}
reader.close();
}
public static void process(int startIndex){
PriorityQueue<Integer> pq=new PriorityQueue<Integer>();
pq.add(startIndex);
nodes[startIndex].len=0;
nodes[startIndex].shortWeight=0;
nodes[startIndex].pid=startIndex;
while(!pq.isEmpty()){
int nodeIndex=pq.poll();
NodePro0731 node=nodes[nodeIndex];
for (int[] tolen:node.sonList ) {
if(nodes[tolen[0]].len > (node.len+tolen[1])){
nodes[tolen[0]].len = (node.len+tolen[1]);
shortWeight[tolen[0]]=tolen[1];
pid[tolen[0]]=nodeIndex;
pq.add(tolen[0]);
}else if(nodes[tolen[0]].len == (node.len+tolen[1])){//距离相同的情况,要使用到达距离最近的节点
if(shortWeight[tolen[0]] >tolen[1]){
shortWeight[tolen[0]]=tolen[1];
pid[tolen[0]]=nodeIndex;
}
}
}
}
}
}
class NodePro0731{
public List<int[]> sonList;
public int len;
public int shortWeight;
public int pid;
public NodePro0731(){
this.sonList=new ArrayList<>();
len=2087654321;
}
}