#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;
}
