#include<bits/stdc++.h>
using namespace std;
struct edge
{
int ind, cost;
bool operator <(const edge & x) const //for priority queue.....
{
return (cost >= x.cost);
}
};
int i , j , k , n , m , a, b, c, d;
vector<edge> g[10100];
int visited[10100];
int dist[10100];
priority_queue<edge> pq;
void djkstra(int s)
{
memset(dist, -1, sizeof(dist));
memset(visited, 0, sizeof(visited));
edge start;
start.ind = s;
start.cost = 0;
pq.push(start);
vector<edge>::iterator it;
dist[s] = 0;
while(!pq.empty())
{
edge x = pq.top();
pq.pop();
if( visited[x.ind] == 1)
continue;
visited[x.ind] = 1;
for( it = g[x.ind].begin(); it != g[x.ind].end(); it++)
{
if( dist[it->ind] < 0 || dist[it->ind] > dist[x.ind] + it->cost )
{
dist[it->ind] = dist[x.ind] + it->cost;
pq.push(*it);
}
}
}
}
int main()
{
int tc;
scanf("%d", &tc);
while(tc--)
{
scanf("%d %d", &n, &m);
for( i = 0 ; i <= n ; i++)
{
g[i].clear();
}
for( i = 0 ; i < m ; i++)
{
scanf("%d%d%d", &a, &b, &c);
edge e1, e2;
e1.ind = a;
e2.ind = b;
e1.cost = e2.cost = c;
g[a].push_back(e2);
}
scanf("%d%d",&a,&b);
djkstra(a);
if( dist[b] == -1)
printf("NO\n");
else
printf("%d\n", dist[b]);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IGVkZ2UKewogICAgaW50IGluZCwgY29zdDsKICAgIGJvb2wgb3BlcmF0b3IgPChjb25zdCBlZGdlICYgeCkgY29uc3QgICAvL2ZvciBwcmlvcml0eSBxdWV1ZS4uLi4uCiAgICB7CiAgICAgICAgcmV0dXJuIChjb3N0ID49IHguY29zdCk7CiAgICB9Cn07CmludCBpICwgaiAsIGsgLCBuICwgbSAsIGEsIGIsIGMsIGQ7CnZlY3RvcjxlZGdlPiBnWzEwMTAwXTsKaW50IHZpc2l0ZWRbMTAxMDBdOwppbnQgZGlzdFsxMDEwMF07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AgICAgIGdbaV0uY2xlYXIoKTsKICAgICAgICB9CiAgICAgICAgZm9yKCBpID0gMCA7IGkgPCBtIDsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgc2NhbmYoIiVkJWQlZCIsICZhLCAmYiwgJmMpOwogICAgICAgICAgICBlZGdlIGUxLCBlMjsKICAgICAgICAgICAgZTEuaW5kID0gYTsKICAgICAgICAgICAgZTIuaW5kID0gYjsKICAgICAgICAgICAgZTEuY29zdCA9IGUyLmNvc3QgPSBjOwogICAgICAgICAgICBnW2FdLnB1c2hfYmFjayhlMik7CiAgICAgICAgfQogICAgICAgIHNjYW5mKCIlZCVkIiwmYSwmYik7CiAgICAgICAgZGprc3RyYShhKTsKICAgICAgICBpZiggZGlzdFtiXSA9PSAtMSkKICAgICAgICAgICAgcHJpbnRmKCJOT1xuIik7CiAgICAgICAgZWxzZQogICAgICAgICAgICBwcmludGYoIiVkXG4iLCBkaXN0W2JdKTsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==