/*
written by: Deinier Morales
language: c++
country: Cuba
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for( __typeof ((c).begin()) i = (c).begin() ; i != (c).end() ; ++i)
#define ALL(c) (c).begin(),(c).end()
#define INF 1000000000
typedef long long Weight;
struct Edge
{
public:
int src, dst;
Weight weight;
Edge()
{src=0;dst=0;weight=0;}
Edge(int psrc, int pdst, Weight pweight)
{src=psrc;dst=pdst;weight=pweight;}
};
bool operator < (const Edge &f,const Edge &g)
{
return f.weight != g.weight ? f.weight > g.weight : f.src != g.src ? f.src < g.src : f.dst < g.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
int c,n,a,x1,y,z,ini,fin;
vector<Weight> dist;
vector<int> prev;
vector<int> cam;
vector<int>::iterator iter;
priority_queue<Edge> Q;
Edges x;
vector<Edge>::iterator iter1;
int main()
{
scanf("%d",&c);
while(c--)
{
scanf("%d%d%d%d",&n,&a,&ini,&fin);
Graph g(n+1);
REP(i,a)
{
scanf("%d%d%d",&x1,&y,&z);
g[x1].push_back(Edge(x1,y,z));
g[y].push_back(Edge(y,x1,z));
}
dist.assign(n+1,INF);
dist[ini] = 0;
prev.assign(n+1,-1);
for(Q.push(Edge(-2,ini,0));!Q.empty();)
{
Edge e = Q.top();
Q.pop();
if(prev[e.dst] != -1)
continue ;
prev[e.dst] = e.src;
x = g[e.dst];
iter1 = x.begin();
while(iter1!=x.end())
{
if(dist[iter1->dst] > e.weight+iter1->weight)
{
dist[iter1->dst] = e.weight + iter1->weight;
Q.push(Edge(iter1->src, iter1->dst, e.weight + iter1->weight));
}
iter1++;
}
}
if(dist[fin] == INF)
printf("NONE\n");
else
printf("%ld\n",dist[fin]);
}
return 0;
}
LyoKd3JpdHRlbiBieTogRGVpbmllciBNb3JhbGVzCmxhbmd1YWdlOiBjKysKY291bnRyeTogQ3ViYQoqLwojaW5jbHVkZS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AgIFEucHVzaChFZGdlKGl0ZXIxLT5zcmMsIGl0ZXIxLT5kc3QsIGUud2VpZ2h0ICsgaXRlcjEtPndlaWdodCkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgaXRlcjErKzsKICAgICAgICAgfQogICAgICB9CgogICAgIGlmKGRpc3RbZmluXSA9PSBJTkYpCiAgICAgICBwcmludGYoIk5PTkVcbiIpOwogICAgIGVsc2UKICAgICAgIHByaW50ZigiJWxkXG4iLGRpc3RbZmluXSk7CiAgIH0KICByZXR1cm4gMDsKfQ==