#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING

#include <bits/stdc++.h>

using namespace std;
const int N = 200100;
int adj[N], nxt[N], to[N], len[N], ecnt;
int dis[N][2];

struct node {
    int p, w;

    bool operator<(const node &rhs) const { return w > rhs.w; }
};

inline void addEdge(int f, int t, int l) {
    ecnt++;
    nxt[ecnt] = adj[f];
    adj[f] = ecnt;
    to[ecnt] = t;
    len[ecnt] = l;
}

int main() {
    int T, n, m;
    scanf("%d", &T);
    while (T--) {
        memset(dis, 0x3f, sizeof(dis));
        scanf("%d%d", &n, &m);
        for (int i = 0, u, v, w; i < m; i++)
            scanf("%d%d%d", &u, &v, &w), addEdge(u, v, w), addEdge(v, u, w);
        priority_queue<node> H;
        dis[1][0] = 0;
        H.push({1, 0});
        while (!H.empty()) {
            auto x = H.top();
            H.pop();
            for (int e = adj[x.p]; e; e = nxt[e]) {
                if (dis[to[e]][0] >= dis[x.p][0] + len[e]) {
                    dis[to[e]][1] = dis[to[e]][0];
                    dis[to[e]][0] = dis[x.p][0] + len[e];
                    H.push({to[e], dis[to[e]][0]});
                } else if (dis[to[e]][1] >= dis[x.p][0] + len[e])
                    dis[to[e]][1] = dis[x.p][0] + len[e];
            }
        }
        printf("%d\n", dis[n][1]);
    }
    return 0;
}
