/*
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;
}