/*
written by- Piyush Golani
language- c++
country- India
College- N.I.T Jamshedpur
*/
#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<map>
#include<cctype>
#include<queue>
using namespace std;
#define pb push_back
#define all(s) s.begin(),s.end()
#define f(i,a,b) for(int i=a;i<b;i++)
#define F(i,a,b) for(int i=a;i>=b;i--)
#define PI 3.1415926535897932384626433832795
#define INF 1000000005
#define BIG_INF 7000000000000000000LL
#define mp make_pair
#define eps 1e-9
#define LL long long
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define mod 1000000007
#define vv pair<int,int>
int main()
{
int t;
si(t);
while(t--)
{
int n,m,s,d;
si(n);
si(m);
si(s);
si(d);
LL ans[n+1];
//memset(ans,INF,sizeof(ans));
f(i,1,n+1) ans[i]=INF;
ans[s]=0;
bool visited[n+1];
memset(visited,false,sizeof(visited));
vector<vv> G[n+1];
int a,b,c;
f(i,0,m)
{
si(a); si(b); si(c);
G[a].pb(mp(b,c));
G[b].pb(mp(a,c));
}
int u=s;
visited[u]=true;
int p;
while(1)
{
int j=INF,k=0;
f(i,0,G[u].size())
{
//cout<<G[u][i].first<<"\n";
if(visited[G[u][i].first]==true) continue;
k++;
ans[G[u][i].first]=min(ans[G[u][i].first],ans[u]+G[u][i].second);
if(ans[G[u][i].first]<j)
{
j=ans[G[u][i].first];
p=G[u][i].first;
}
}
if(k==0) break;
u=p;
visited[u]=true;
if(u==d)
{
printf("%lld\n",ans[u]);
break;
}
}
//cout<<ans[d]<<"\n";
if(ans[d]==INF)
{
printf("NONE\n");
}
}
return 0;
}
LyoKd3JpdHRlbiBieS0gUGl5dXNoIEdvbGFuaQpsYW5ndWFnZS0gYysrCmNvdW50cnktIEluZGlhCkNvbGxlZ2UtIE4uSS5UIEphbXNoZWRwdXIKKi8KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3RpbWU+CiNpbm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k7CiAgICAgICAgZihpLDEsbisxKSBhbnNbaV09SU5GOwogICAgICAgIGFuc1tzXT0wOwogICAgICAgIGJvb2wgdmlzaXRlZFtuKzFdOwogICAgICAgIG1lbXNldCh2aXNpdGVkLGZhbHNlLHNpemVvZih2aXNpdGVkKSk7CiAgICAgICAgdmVjdG9yPHZ2PiBHW24rMV07CiAgICAgICAgaW50IGEsYixjOwogICAgICAgIGYoaSwwLG0pCiAgICAgICAgewogICAgICAgICAgICBzaShhKTsgc2koYik7IHNpKGMpOwogICAgICAgICAgICBHW2FdLnBiKG1wKGIsYykpOwogICAgICAgICAgICBHW2JdLnBiKG1wKGEsYykpOwogICAgICAgIH0KICAgICAgICBpbnQgdT1zOwogICAgICAgIHZpc2l0ZWRbdV09dHJ1ZTsKICAgICAgICBpbnQgcDsKCiAgICAgICAgd2hpbGUoMSkKICAgICAgICB7CiAgICAgICAgaW50IGo9SU5GLGs9MDsKICAgICAgICBmKGksMCxHW3VdLnNpemUoKSkKICAgICAgICB7CiAgICAgICAgICAgIC8vY291dDw8R1t1XVtpXS5maXJzdDw8IlxuIjsKICAgICAgICAgICAgaWYodmlzaXRlZFtHW3VdW2ldLmZpcnN0XT09dHJ1ZSkgY29udGludWU7CiAgICAgICAgICAgIGsrKzsKICAgICAgICAgICAgYW5zW0dbdV1baV0uZmlyc3RdPW1pbihhbnNbR1t1XVtpXS5maXJzdF0sYW5zW3VdK0dbdV1baV0uc2Vjb25kKTsKICAgICAgICAgICAgaWYoYW5zW0dbdV1baV0uZmlyc3RdPGopCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGo9YW5zW0dbdV1baV0uZmlyc3RdOwogICAgICAgICAgICAgICAgcD1HW3VdW2ldLmZpcnN0OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKGs9PTApIGJyZWFrOwogICAgICAgIHU9cDsKICAgICAgICAgdmlzaXRlZFt1XT10cnVlOwogICAgICAgICBpZih1PT1kKQogICAgICAgICB7CiAgICAgICAgICAgICBwcmludGYoIiVsbGRcbiIsYW5zW3VdKTsKICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICB9CiAgICB9CiAgICAvL2NvdXQ8PGFuc1tkXTw8IlxuIjsKICAgIGlmKGFuc1tkXT09SU5GKQogICAgewogICAgICAgIHByaW50ZigiTk9ORVxuIik7CiAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQo=