#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define ll long long
#define endl '\n'
int DX[]= {0,0,1,-1};
int DY[]= {1,-1,0,0};
bool vis[105][105][2] {false};
const int N=1e5+10;
int dis[N];
vector<pair<int,int>>adj[N];
void init()
{
for(int i=0; i<N; i++)
{
dis[i]=1e9;
}
}
void Dijkstra(int src) // e log v
{
dis[src]=0;
//dis,node
priority_queue<pair<int,int>>Q;
Q.push({-dis[src],src});
while(Q.size())
{
// AUTO take best candidate
int u=Q.top().second;
int d=-Q.top().first;
Q.pop();
// cout<<u<<endl;
if(d!=dis[u])
{
continue;
}
// cout<<"pass "<<u<<endl;
// relaxation
for( auto x: adj[u])
{
// cout<<x.first<<" ";
int v=x.first;
int c=x.second;
int newCost=d+c;
// cout<<newCost<<" "<<dis[v]<<endl;
if(newCost<dis[v])
{
dis[v]=newCost;
Q.push({-newCost,v});
}
}
// cout<<endl;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,src,dist;
cin>>n>>m>>src>>dist;
for(int i=0;i<m;i++)
{
int f,t,c;
cin>>f>>t>>c;
adj[f].push_back({t,c});
adj[t].push_back({f,c});
}
init();
Dijkstra(src);
if(dis[dist]==1e9)
{
cout<<"NONE"<<endl;
}
else
{
cout<<dis[dist]<<endl;
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIEZJTyBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApLGNpbi50aWUoMCksY291dC50aWUoMCk7CgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGVuZGwgJ1xuJwoKaW50IERYW109IHswLDAsMSwtMX07CmludCBEWVtdPSB7MSwtMSwwLDB9Owpib29sIHZpc1sxMDVdWzEwNV1bMl0ge2ZhbHNlfTsKCmNvbnN0IGludCBOPTFlNSsxMDsKaW50IGRpc1tOXTsKdmVjdG9yPHBhaXI8aW50LGludD4+YWRqW05dOwp2b2lkIGluaXQoKQp7CiAgICBmb3IoaW50IGk9MDsgaTxOOyBpKyspCiAgICB7CiAgICAgICBkaXNbaV09MWU5OwogICAgfQoKCn0KCgp2b2lkIERpamtzdHJhKGludCBzcmMpIC8vIGUgbG9nIHYKewoKICAgIGRpc1tzcmNdPTA7CiAgICAgICAgLy9kaXMsbm9kZQogICAgcHJpb3JpdHlfcXVldWU8cGFpcjxpbnQsaW50Pj5ROwoKICAgIFEucHVzaCh7LWRpc1tzcmNdLHNyY30pOwoKICAgIHdoaWxlKFEuc2l6ZSgpKQogICAgewogICAgICAgICAgICAvLyBBVVRPIHRha2UgYmVzdCBjYW5kaWRhdGUKICAgICAgICBpbnQgdT1RLnRvcCgpLnNlY29uZDsKICAgICAgICBpbnQgZD0tUS50b3AoKS5maXJzdDsKICAgICAgICBRLnBvcCgpOwogICAgICAvLyAgY291dDw8dTw8ZW5kbDsKICAgICAgICBpZihkIT1kaXNbdV0pCiAgICAgICAgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CiAgICAgICAgIC8vICAgY291dDw8InBhc3MgICI8PHU8PGVuZGw7CiAgICAgICAgLy8gcmVsYXhhdGlvbgoKCiAgICAgICAgZm9yKCBhdXRvIHg6IGFkalt1XSkKICAgICAgICB7CiAgICAgICAgICAvLyAgY291dDw8eC5maXJzdDw8IiAiOwogICAgICAgICAgICBpbnQgdj14LmZpcnN0OwogICAgICAgICAgICBpbnQgYz14LnNlY29uZDsKCiAgICAgICAgICAgIGludCBuZXdDb3N0PWQrYzsKICAgICAgICAgICAvLyBjb3V0PDxuZXdDb3N0PDwiICI8PGRpc1t2XTw8ZW5kbDsKICAgICAgICAgICAgaWYobmV3Q29zdDxkaXNbdl0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGRpc1t2XT1uZXdDb3N0OwogICAgICAgICAgICAgICAgUS5wdXNoKHstbmV3Q29zdCx2fSk7CiAgICAgICAgICAgIH0KCiAgICAgICAgfQogICAgICAgLy8gY291dDw8ZW5kbDsKCiAgICB9CgoKfQoKCgppbnQgbWFpbigpCnsKCiAgICBpbnQgdDsKICAgIGNpbj4+dDsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBpbnQgbixtLHNyYyxkaXN0OwogICAgICAgIGNpbj4+bj4+bT4+c3JjPj5kaXN0OwogICAgICAgIGZvcihpbnQgaT0wO2k8bTtpKyspCiAgICAgICAgewogICAgICAgICAgICBpbnQgZix0LGM7CiAgICAgICAgICAgIGNpbj4+Zj4+dD4+YzsKICAgICAgICAgICAgYWRqW2ZdLnB1c2hfYmFjayh7dCxjfSk7CiAgICAgICAgICAgIGFkalt0XS5wdXNoX2JhY2soe2YsY30pOwogICAgICAgIH0KICAgICAgICBpbml0KCk7CiAgICAgICAgRGlqa3N0cmEoc3JjKTsKICAgICAgICBpZihkaXNbZGlzdF09PTFlOSkKICAgICAgICB7CiAgICAgICAgICAgIGNvdXQ8PCJOT05FIjw8ZW5kbDsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgY291dDw8ZGlzW2Rpc3RdPDxlbmRsOwogICAgICAgIH0KICAgIH0KCn0K