#include<stdio.h>
#include<queue>

using namespace std;

#define MAXN 100
#define INF 99999


queue<int>Q;

int  Cost[MAXN];
int  W[MAXN][MAXN];
char Link[MAXN][MAXN];
char F[MAXN];
int N, E;

void Dij(int st) {
	int i, u, cost;
	int temp;
	for(i = 1; i<= N; i++) 
		Cost[i] = INF;
	Cost[st] = 0;
	temp= st;
	Q.push(temp);
	F[st] = 1;
	while(!Q.empty()) {
		u  = Q.front();
		Q.pop();
		F[u] = 0;
		for(i = 1; i <= N; i++) {
			if(Link[u][i] == 1) {
				cost = W[u][i] + Cost[u];
				if(cost>= Cost[i]) continue;
				Cost[i] = cost;
				if(F[i] == 0) {
					Q.push(i);
					F[i] = 1;
				}
			}
		}
	}
}

void Cal() {
	int st, q, n;
	scanf("%d",&st);
	Dij(st);
	while(scanf("%d",&n) && n) {
		if(Cost[n] == INF) printf("No Path\n");
		else
			printf("%d\n",Cost[n]);
	}
}

void main() {
	int  u, v,cost;
	scanf("%d%d",&N,&E);
	while(E--) {
		scanf("%d%d%d",&u,&v,&cost);
		Link[u][v] = 1;
		W[u][v] = cost;
	}
	Cal();
}
