#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3fll, N = 1e4 + 50, M = 4e4 + 50;
ll adj[N], nxt[M], to[M], len[M], cap[M], dis[N], ecnt, idx;
inline void addEdge(int f, int t, int l)
{
    nxt[ecnt] = adj[f];
    adj[f] = ecnt;
    to[ecnt] = t;
    len[ecnt] = l;
    ecnt++;
}
inline void addEdge_impl_(int f, int t, int c)
{
    nxt[ecnt] = adj[f];
    adj[f] = ecnt;
    to[ecnt] = t;
    cap[ecnt] = c;
    ecnt++;
}
inline void addEdge_F(int f, int t, int c)
{
    addEdge_impl_(f, t, c);
    addEdge_impl_(t, f, 0);
}
struct edge
{
    ll f, t, l;
    bool operator<(const edge &rhs) const { return l > rhs.l; }
} E[M];
struct heap_node
{
    int u, d;
    heap_node(int _u = 0, int _d = 0) { u = _u, d = _d; }
    bool operator<(const heap_node &rhs) const { return d > rhs.d; }
};
priority_queue<heap_node> H;
int cnt[N], cur[N], fa[N];
ll ISAP(int S, int T)
{
    ll flow = 0;
    for (int i = 0; i < N; i++) dis[i] = N - 1;
    int len = 0, x;
    static int que[N];
    dis[que[len++] = T] = 0;
    for (int i = 0; i < len; i++)
        for (int e = adj[x = que[i]]; ~e; e = nxt[e])
            if (cap[e ^ 1] && dis[to[e]] > dis[x] + 1)
                dis[que[len++] = to[e]] = dis[x] + 1;
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < N; i++) cur[i] = adj[i], cnt[dis[i]]++;
    x = S;
    while (dis[S] < N - 1)
    {
        if (x == T)
        {
            ll curFlow = inf;
            for (x = T; x != S; x = to[fa[x] ^ 1]) curFlow = min(curFlow, cap[fa[x]]);
            for (x = T; x != S; x = to[fa[x] ^ 1]) cap[fa[x]] -= curFlow, cap[fa[x] ^ 1] += curFlow;
            flow += curFlow, x = S;
        }
        bool needRetreat = true;
        for (int e = cur[x]; needRetreat && ~e; e = nxt[e])
            if (cur[x] = e, cap[e] && dis[x] == dis[to[e]] + 1)
                needRetreat = false, fa[x = to[e]] = e;
        if (needRetreat)
        {
            ll nd = N - 2;
            for (int e = adj[x]; ~e; e = nxt[e])
                if (cap[e]) nd = min(nd, dis[to[e]]);
            if (--cnt[dis[x]] == 0) break;
            ++cnt[dis[x] = nd + 1];
            cur[x] = adj[x];
            if (x != S) x = to[fa[x] ^ 1];
        }
    }
    return flow;
}
int main()
{
    int Tt;
    scanf("%d", &Tt);
    while (Tt--)
    {
        memset(adj, -1, sizeof(adj)), ecnt = 0;
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 0; i < m; i++)
            scanf("%d%d%d", &E[i].f, &E[i].t, &E[i].l);
        if (n == 1)
        {
            puts("0");
            continue;
        }
        sort(E, E + m);
        for (int i = 0; i < m; i++) addEdge(E[i].f, E[i].t, E[i].l);
        memset(dis, 0x3f, sizeof(dis));
        H.push(heap_node(1, dis[1] = 0));
        for (heap_node cur; !H.empty(); H.pop())
            for (int e = adj[(cur = H.top()).u]; ~e; e = nxt[e])
                if (dis[to[e]] > dis[cur.u] + len[e])
                    H.push(heap_node(to[e], dis[to[e]] = dis[cur.u] + len[e]));
        memset(adj, -1, sizeof(adj)), ecnt = 0;
        for (int i = 0; i < m; i++)
            if (dis[E[i].t] == dis[E[i].f] + E[i].l)
                addEdge_F(E[i].f, E[i].t, E[i].l);
        printf("%lld\n", ISAP(1, n));
    }
    return 0;
}