#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a) a.begin(),a.end()
#define SL(n) scanf("%d",&n)
#define PL(n) printf("%lld",n)
#define fill(a, x) memset(a,x,sizeof(a));
#define mod 1000000007
using namespace std;
typedef int LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;
int main(){
LL T;
SL(T);
LL x, y, z;
while(T--){
LL N, M;
SL(N);
SL(M);
vector<vector<LL> > Undirected;
map<pair<LL, LL>, LL> Cost;
Undirected.resize(N+5);
for(LL m=0;m<M;++m){
SL(x);
SL(y);
SL(z);
Cost[mp(x, y)] = z;
Undirected[x].pb(y);
}
LL start, end;
scanf("%d %d", &start, &end);
set<pair<LL, LL> > Q;
Q.insert(mp((LL)0, start));
bool flag = 0;
bool Vis[N+5];
LL Dist[N+5];
for(LL n=1;n<=N;++n){
Vis[n] = 0;
Dist[n] = 1e9;
}
Dist[start] = 0;
while(Q.sz > 0){
x=Q.begin()->second;
y=Q.begin()->first;
Q.erase(Q.begin());
if(x == end) {
flag = 1;
break;
}
for(LL n=0;n<Undirected[x].sz;++n){
if(Vis[Undirected[x][n]] == 0 || Vis[Undirected[x][n]] == 1 && Dist[Undirected[x][n]] > Dist[x] + Cost[mp(x, Undirected[x][n])]){// Graph[x][Undirected[x][n]]){
if(Vis[Undirected[x][n]] == 1){
Q.erase(Q.find(mp(Dist[Undirected[x][n]], Undirected[x][n])));
}
else{
Vis[Undirected[x][n]] = 1;
}
Dist[Undirected[x][n]] = Dist[x] + Cost[mp(x, Undirected[x][n])];
Q.insert(mp(Dist[Undirected[x][n]], Undirected[x][n]));
}
}
}
if(flag == 0){
printf("NO\n");
}
else{
printf("%d\n", Dist[end]);
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBmZiBmaXJzdAojZGVmaW5lIHNzIHNlY29uZAojZGVmaW5lIHN6IHNpemUoKQojZGVmaW5lIGFsbChhKSAgYS5iZWdpbigpLGEuZW5kKCkgCiNkZWZpbmUgU0wobikgc2NhbmYoIiVkIiwmbikKI2RlZmluZSBQTChuKSBwcmludGYoIiVsbGQiLG4pCiNkZWZpbmUgZmlsbChhLCB4KSBtZW1zZXQoYSx4LHNpemVvZihhKSk7CiNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgaW50IExMOwp0eXBlZGVmIHZlY3RvciA8TEw+IFZMOwp0eXBlZGVmIG1hcCA8TEwsIExMPiBNTDsKdHlwZWRlZiBwYWlyPExMLCBMTD4gUEw7CnR5cGVkZWYgdmVjdG9yIDxwYWlyIDxMTCwgTEw+ID4gVlBMOwoKaW50IG1haW4oKXsKICAgIExMIFQ7CiAgICBTTChUKTsKICAgIExMIHgsIHksIHo7CiAgICB3aGlsZShULS0pewogICAgICAgIExMIE4sIE07CiAgICAgICAgU0woTik7CiAgICAgICAgU0woTSk7CiAgICAgICAgdmVjdG9yPHZlY3RvcjxMTD4gPiBVbmRpcmVjdGVkOwogICAgICAgIG1hcDxwYWlyPExMLCBMTD4sIExMPiBDb3N0OwogICAgICAgIFVuZGlyZWN0ZWQucmVzaXplKE4rNSk7CiAgICAgICAgZm9yKExMIG09MDttPE07KyttKXsKICAgICAgICAgICAgU0woeCk7CiAgICAgICAgICAgIFNMKHkpOwogICAgICAgICAgICBTTCh6KTsKICAgICAgICAgICAgQ29zdFttcCh4LCB5KV0gPSB6OwogICAgICAgICAgICBVbmRpcmVjdGVkW3hdLnBiKHkpOwogICAgICAgIH0KICAgICAgICBMTCBzdGFydCwgZW5kOwogICAgICAgIHNjYW5mKCIlZCAlZCIsICZzdGFydCwgJmVuZCk7CiAgICAgICAgc2V0PHBhaXI8TEwsIExMPiA+IFE7CiAgICAgICAgUS5pbnNlcnQobXAoKExMKTAsIHN0YXJ0KSk7CiAgICAgICAgYm9vbCBmbGFnID0gMDsKICAgICAgICBib29sIFZpc1tOKzVdOwogICAgICAgIExMIERpc3RbTis1XTsKICAgICAgICBmb3IoTEwgbj0xO248PU47KytuKXsKICAgICAgICAgICAgVmlzW25dID0gMDsKICAgICAgICAgICAgRGlzdFtuXSA9IDFlOTsKICAgICAgICB9CiAgICAgICAgRGlzdFtzdGFydF0gID0gMDsKICAgICAgICB3aGlsZShRLnN6ID4gMCl7CiAgICAgICAgICAgIHg9US5iZWdpbigpLT5zZWNvbmQ7CiAgICAgICAgICAgIHk9US5iZWdpbigpLT5maXJzdDsKICAgICAgICAgICAgUS5lcmFzZShRLmJlZ2luKCkpOwogICAgICAgICAgICBpZih4ID09IGVuZCkgewogICAgICAgICAgICAgICAgZmxhZyA9IDE7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IoTEwgbj0wO248VW5kaXJlY3RlZFt4XS5zejsrK24pewogICAgICAgICAgICAgICAgaWYoVmlzW1VuZGlyZWN0ZWRbeF1bbl1dID09IDAgfHwgVmlzW1VuZGlyZWN0ZWRbeF1bbl1dID09IDEgJiYgRGlzdFtVbmRpcmVjdGVkW3hdW25dXSA+IERpc3RbeF0gKyBDb3N0W21wKHgsIFVuZGlyZWN0ZWRbeF1bbl0pXSl7Ly8gIEdyYXBoW3hdW1VuZGlyZWN0ZWRbeF1bbl1dKXsKICAgICAgICAgICAgICAgICAgICBpZihWaXNbVW5kaXJlY3RlZFt4XVtuXV0gPT0gMSl7CiAgICAgICAgICAgICAgICAgICAgICAgIFEuZXJhc2UoUS5maW5kKG1wKERpc3RbVW5kaXJlY3RlZFt4XVtuXV0sIFVuZGlyZWN0ZWRbeF1bbl0pKSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgIFZpc1tVbmRpcmVjdGVkW3hdW25dXSA9IDE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIERpc3RbVW5kaXJlY3RlZFt4XVtuXV0gPSBEaXN0W3hdICsgQ29zdFttcCh4LCBVbmRpcmVjdGVkW3hdW25dKV07CiAgICAgICAgICAgICAgICAgICAgUS5pbnNlcnQobXAoRGlzdFtVbmRpcmVjdGVkW3hdW25dXSwgVW5kaXJlY3RlZFt4XVtuXSkpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKGZsYWcgPT0gMCl7CiAgICAgICAgICAgIHByaW50ZigiTk9cbiIpOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBwcmludGYoIiVkXG4iLCBEaXN0W2VuZF0pOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==