#include<bits/stdc++.h>
using namespace std;
#define INF 10000000000000009
#define MAX 100009
#define pii pair <long long ,long long >
#define pb push_back
vector < pii > graph[MAX];
long long dist[MAX];
void Reset()
{
memset(dist,INF,sizeof(dist));
long long i;
for(i=0;i<MAX;i++)
graph[i].clear();
}
void dijkstra(long long start)
{
priority_queue < pii , vector< pii >,greater< pii > > q; //in the priority queue the greater func is default
dist[start]=0;
q.push(pii(0,start)); //pushing the starting node
long long i,v,w,u,c;
while(!q.empty())
{
u=q.top().second;
c=q.top().first;
q.pop();
for(i=0;i<graph[u].size();i++)
{
v=graph[u][i].first;
w=graph[u][i].second;
if(dist[v]>dist[u]+w)
{
dist[v]=dist[u]+w;
q.push(pii(dist[v],v));
}
}
}
}
int main()
{
long long i,n,v,u,w,edges,nodes,t,start,destination;
scanf("%lld",&t);
while(t--)
{
Reset();
scanf("%lld%lld",&nodes,&edges);
for(i=1;i<=edges;i++)
{
scanf("%lld%lld%lld",&v,&u,&w); //taking the edges as input in a array of paired vectors containing relation,dist
//this a directed graph ....make appropriate changes for an undirected graph
graph[v].pb(pii(u,w));
}
scanf("%lld%lld",&start,&destination);
dijkstra(start);
if(dist[destination]>=INF)
printf("NO\n");
else
printf("%lld\n",dist[destination]);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBJTkYgMTAwMDAwMDAwMDAwMDAwMDkKI2RlZmluZSBNQVggMTAwMDA5CiNkZWZpbmUgcGlpIHBhaXIgPGxvbmcgbG9uZyAsbG9uZyBsb25nID4KI2RlZmluZSBwYiBwdXNoX2JhY2sKdmVjdG9yIDwgcGlpID4gZ3JhcGhbTUFYXTsgCmxvbmcgbG9uZyBkaXN0W01BWF07CnZvaWQgUmVzZXQoKQp7CgltZW1zZXQoZGlzdCxJTkYsc2l6ZW9mKGRpc3QpKTsKCWxvbmcgbG9uZyBpOwoJZm9yKGk9MDtpPE1BWDtpKyspCglncmFwa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