//Shirasu Azusa 2024.6
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
return x*w;
}
const int N=5e5+5,inf=0x3f3f3f3f3f3f3f3f;
int n,m,g[N],ans,sp,vst[N],id[N],f[N];
struct ed {int u,v,w;} e[N];
vi p[N];
void work() {
n=read(), m=read();
rep(i,1,n) p[i].clear(), f[i]=0;
rep(i,1,m) {
e[i].u=read(), e[i].v=read(), e[i].w=read();
if(e[i].u>e[i].v) sp=i; p[e[i].u].eb(i);
}
ans=0;
rep(i,1,n) g[i]=0; e[0].w=inf;
rep(i,1,m) {
if(e[g[e[i].v]].w>e[i].w) {
g[e[i].v]=i;
}
}
bool flag=0;
rep(i,1,n) if(g[i]) ans+=e[g[i]].w;
rep(i,1,n) flag|=(g[i]==sp);
if(flag) {
rep(i,2,n) f[i]=inf;
rep(i,1,n) {
for(int j:p[i]) chmin(f[e[j].v],f[i]+e[j].w-e[g[e[j].v]].w);
}
}z
printf("%lld\n",ans+f[e[sp].u]);
}
signed main() {
int T=read(); while(T--) work();
return 0;
}