eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#define MAX 10000
 
using namespace std;
 
int cnt;//0
int lastshow[MAX];//i,-1
int sum;
int vis[MAX];
 
struct edge{   //a->b
    int to;    //b
    int pro;  //a
    int weight;//
}e[MAX];
 
void insert(){
    int a, b, weight;
    scanf("%d %d %d", &a, &b, &weight);
    e[cnt].to = b;
    e[cnt].pro = lastshow[a]; //a
    e[cnt].weight = weight;
    lastshow[a] = cnt ++; //lastshow
}
 
int dfs(int a){
    if(vis[a])
        return 0;
//    cout << endl << a << endl;
    vis[a] = 1;
    int ret = 0;
    int id = lastshow[a];
    while(id != -1){
        ret += e[id].weight;
//        cout << endl << e[id].weight << endl;
        ret += dfs(e[id].to);
        id = e[id].pro;
    }
    return ret;
}
 
int main(){
    int n, c, srch;
    int i, j, k;
    while(cin >> n >> c){
        cnt = 0;
        sum = 0;
        memset(vis, 0, sizeof(vis));
        for(i = 0; i < MAX; i ++){
            lastshow[i] = -1;
        }
        for(i = 0; i < c; i ++){
            insert();
        }
        //i
        while(~scanf("%d", &srch)){
            if(-1 == srch)
                break;
            int id = lastshow[srch];
            cout << "" << srch << ":"<<endl;
            while(id != -1){
                cout << e[id].to << " " << e[id].weight << endl;
                id = e[id].pro;
            }
        }
        cout << "" << dfs(0) << endl;
    }
    return 0;
}