#include <bits/stdc++.h> #define MAXN 10005 using namespace std; typedef long long LL; vector<pair<int,LL>> edges[MAXN]; const LL oo=1e18; int n,m; LL d[MAXN]; void init(){ for(int i=0;i<MAXN;i++)edges[i].clear(); for(int i=0;i<MAXN;i++)d[i]=oo; } int main(){ freopen("test1.in","r",stdin); std::ios::sync_with_stdio(0); std::cin.tie(0); int cases; scanf("%d",&cases); while(cases--){ init(); scanf("%d%d",&n,&m); while(m--){ int x,y; LL z; scanf("%d%d%lld",&x,&y,&z); edges[x].push_back(make_pair(y,z)); } priority_queue<pair<int,int>> que; d[1]=0; que.push(make_pair(-d[1],1)); while(!que.empty()){ int now = que.top().second; que.pop(); for(int i=0;i<edges[now].size();i++){ int v = edges[now][i].first; if(d[v]>d[now]+edges[now][i].second){ d[v] = d[now]+edges[now][i].second; que.push(make_pair(-d[v],v)); } } } printf("%lld ",d[n]); } return 0; }