#include<bits/stdc++.h>
using namespace std;
int adj[101][101];
vector < pair < int , int > > Orders;
typedef pair < int , int > ii ;
typedef pair < int , ii > iii ;
int dp[1<<14][104];
const int INF = (int)1e9;
int main()
{
int t;
scanf("%d",&t);
for(;t;--t)
{
int n , m , b , i , j , u , v , d ;
int z , k ;
int bee;
scanf("%d%d%d",&n,&m,&bee);
const int start = bee;
for(i=0;i<n;++i)
for(j=0;j<n;++j)
{
if(i!=j)
adj[i][j] = INF;
else
adj[i][j] = 0 ;
}
for(i=0;i<m;++i)
{
scanf("%d%d%d",&u,&v,&d);
adj[u-1][v-1] = min(d,adj[u-1][v-1]);
adj[v-1][u-1] = adj[u-1][v-1];
}
for(k = 0;k<n;++k)
for(i=0;i<n;++i)
for(j=0;j<n;++j)
adj[i][j] = min(adj[i][j] , adj[i][k] + adj[k][j]);
cout<<"\n";
for(i=0;i<n;++i,cout<<endl)
for(j=0;j<n;++j)
cout<<adj[i][j]<<" ";
scanf("%d",&z);
for(i=0;i<z;++i)
{
scanf("%d%d%d",&u,&v,&d);
for(j=0;j<d;++j)
Orders.push_back(make_pair(u-1,v-1));
}
int sz = (int)Orders.size();
for(i=0;i<=sz;++i)
for(j=0;j<=(1<<sz);++j)
dp[i][j] = INF;
for(i=0;i<sz;++i)
dp[1<<i][i] = adj[start][Orders[i].first] + adj[Orders[i].first][Orders[i].second];
for(i=1; i <(1<<sz) ;++i)
{
for(j=0;j<sz;++j)
{
if( __builtin_popcount(i)==1 )
{
if(!(i&(1<<j)))
dp[i][j] = INF;
continue;
}
dp[i][j] = INF;
if( i&(1<<j) )
{
int left = i^(1<<j);
for(k=0;k<sz;++k)
{
if(left&(1<<k))
dp[i][j] = min(dp[i][j] , dp[left][k] + adj[Orders[k].second][Orders[j].first] + adj[Orders[j].first][Orders[j].second]);
}
}
}
}
int ans = INF;
for(i=0;i<sz;++i)
ans = min(ans , dp[(1<<sz)-1][i] + adj[Orders[i].second][start]);
printf("%d\n",ans);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGFkalsxMDFdWzEwMV07CnZlY3RvciA8IHBhaXIgPCBpbnQgLCBpbnQgPiA+IE9yZGVyczsKdHlwZWRlZiBwYWlyIDwgaW50ICwgaW50ID4gaWkgOwp0eXBlZGVmIHBhaXIgPCBpbnQgLCBpaSA+IGlpaSA7CgppbnQgZHBbMTw8MTRdWzEwNF07Cgpjb25zdCBpbnQgSU5GID0gKGludCkxZTk7CgppbnQgbWFpbigpCnsKICAgIGludCB0OwogICAgc2NhbmYoIiVkIiwmdCk7CiAgICBmb3IoO3Q7LS10KQogICAgewogICAgICAgIGludCBuICwgbSAsIGIgLCAgaSAsIGogICwgdSAsIHYgLCBkIDsKICAgICAgICBpbnQgeiAgLCBrIDsKICAgICAgICBpbnQgYmVlOwogICAgICAgIHNjYW5mKCIlZCVkJWQiLCZuLCZtLCZiZWUpOwogICAgICAgIGNvbnN0IGludCBzdGFydCA9IGJlZTsKCgogICAgICAgIGZvcihpPTA7aTxuOysraSkKICAgICAgICAgICAgZm9yKGo9MDtqPG47KytqKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGlmKGkhPWopCiAgICAgICAgICAgICAgICAgICAgICAgIGFkaltpXVtqXSA9IElORjsKICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgIGFkaltpXVtqXSA9IDAgOwogICAgICAgICAgICAgICAgfQoKICAgICAgICBmb3IoaT0wO2k8bTsrK2kpCiAgICAgICAgewogICAgICAgICAgICBzY2FuZigiJWQlZCVkIiwmdSwmdiwmZCk7CiAgICAgICAgICAgIGFkalt1LTFdW3YtMV0gPSBtaW4oZCxhZGpbdS0xXVt2LTFdKTsKICAgICAgICAgICAgYWRqW3YtMV1bdS0xXSA9IGFkalt1LTFdW3YtMV07CiAgICAgICAgfQoKICAgICAgICBmb3IoayA9IDA7azxuOysraykKICAgICAgICAgICAgZm9yKGk9MDtpPG47KytpKQogICAgICAgICAgICAgICAgZm9yKGo9MDtqPG47KytqKQogICAgICAgICAgICAgICAgICAgICAgICBhZGpbaV1bal0gPSBtaW4oYWRqW2ldW2pdICwgYWRqW2ldW2tdICsgYWRqW2tdW2pdKTsKICAgICAgICBjb3V0PDwiXG4iOwoKCiAgICAgICAgZm9yKGk9MDtpPG47KytpLGNvdXQ8PGVuZGwpCiAgICAgICAgICAgIGZvcihqPTA7ajxuOysraikKICAgICAgICAgICAgICAgIGNvdXQ8PGFkaltpXVtqXTw8IiAiOwoKCgoKICAgICAgICBzY2FuZigiJWQiLCZ6KTsKCiAgICAgICAgZm9yKGk9MDtpPHo7KytpKQogICAgICAgIHsKICAgICAgICAgICAgc2NhbmYoIiVkJWQlZCIsJnUsJnYsJmQpOwogICAgICAgICAgICBmb3Ioaj0wO2o8ZDsrK2opCiAgICAgICAgICAgICAgICBPcmRlcnMucHVzaF9iYWNrKG1ha2VfcGFpcih1LTEsdi0xKSk7CiAgICAgICAgfQoKICAgICAgICBpbnQgc3ogPSAoaW50KU9yZGVycy5zaXplKCk7CgoKICAgICAgICBmb3IoaT0wO2k8PXN6OysraSkKICAgICAgICAgICAgZm9yKGo9MDtqPD0oMTw8c3opOysraikKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gSU5GOwoKICAgICAgICBmb3IoaT0wO2k8c3o7KytpKQogICAgICAgICAgICBkcFsxPDxpXVtpXSA9IGFkaltzdGFydF1bT3JkZXJzW2ldLmZpcnN0XSArIGFkaltPcmRlcnNbaV0uZmlyc3RdW09yZGVyc1tpXS5zZWNvbmRdOwoKCiAgICAgICAgZm9yKGk9MTsgaSA8KDE8PHN6KSA7KytpKQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGo9MDtqPHN6OysraikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYoIF9fYnVpbHRpbl9wb3Bjb3VudChpKT09MSApCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZighKGkmKDE8PGopKSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdID0gSU5GOwogICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgZHBbaV1bal0gPSBJTkY7CiAgICAgICAgICAgICAgICBpZiggaSYoMTw8aikgKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGludCBsZWZ0ID0gaV4oMTw8aik7CiAgICAgICAgICAgICAgICAgICAgZm9yKGs9MDtrPHN6OysraykKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKGxlZnQmKDE8PGspKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgZHBbaV1bal0gPSBtaW4oZHBbaV1bal0gLCBkcFtsZWZ0XVtrXSArIGFkaltPcmRlcnNba10uc2Vjb25kXVtPcmRlcnNbal0uZmlyc3RdICsgYWRqW09yZGVyc1tqXS5maXJzdF1bT3JkZXJzW2pdLnNlY29uZF0pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICBpbnQgYW5zID0gSU5GOwoKICAgIGZvcihpPTA7aTxzejsrK2kpCiAgICAgICAgICAgIGFucyA9IG1pbihhbnMgLCBkcFsoMTw8c3opLTFdW2ldICsgYWRqW09yZGVyc1tpXS5zZWNvbmRdW3N0YXJ0XSk7CgogICAgIHByaW50ZigiJWRcbiIsYW5zKTsKCgogICAgfQogICAgcmV0dXJuIDA7Cn0K