// This amazing code is by Eric Sunli Chen. #include using namespace std; template bool get_int(T &x) { char t=getchar(); bool neg=false; x=0; for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar()); if(t=='-')neg=true,t=getchar();if(t==EOF)return false; for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0'; if(neg)x=-x;return true; } template void print_int(T x) { if(x<0)putchar('-'),x=-x; short a[20]= {},sz=0; while(x>0)a[sz++]=x%10,x/=10; if(sz==0)putchar('0'); for(int i=sz-1; i>=0; i--)putchar('0'+a[i]); } #define ff first #define ss second #define pb push_back #define mp make_pair #define get1(a) get_int(a) #define get2(a,b) (get1(a)&&get1(b)) #define get3(a,b,c) (get1(a)&&get2(b,c)) #define printendl(a) print_int(a),puts("") typedef long long LL; typedef unsigned long long uLL; typedef pair pii; const int inf=0x3f3f3f3f; const LL Linf=1ll<<61; const double pi=acos(-1.0); const int mod=998244353; const int maxn=1111; vector g[maxn],l[maxn]; int n,m,dist2[maxn]; LL dist[maxn]; int dijkstra(int vx) { memset(dist,-1,(n+3)*sizeof(LL)); priority_queue >pq; pq.push(mp(0,vx)); dist[vx]=dist2[vx]=0; while(!pq.empty()) { int x=pq.top().ss;LL d=-pq.top().ff;pq.pop(); if(dist[x]!=d)continue; int v=dist2[x];if(x!=vx)v=max(v,x); for(int i=0;i<(int)g[x].size();i++) { if(dist[g[x][i]]==-1||dist[g[x][i]]>d+l[x][i]) { dist[g[x][i]]=d+l[x][i]; dist2[g[x][i]]=v; pq.push(mp(-dist[g[x][i]],g[x][i])); } else if(dist[g[x][i]]==d+l[x][i])dist2[g[x][i]]=min(dist2[g[x][i]],v); } } int ret=0; for(int i=1;i<=n;i++) { ret+=dist2[i]; if(ret>=mod)ret-=mod; } return ret; } void solve() { get2(n,m); for(int i=1;i<=n;i++) { g[i].clear(); l[i].clear(); } for(int i=1,u,v,w;i<=m;i++) { get3(u,v,w); g[u].pb(v);g[v].pb(u);l[u].pb(w);l[v].pb(w); } int ans=0; for(int i=1;i<=n;i++) { ans+=dijkstra(i); if(ans>=mod)ans-=mod; } printf("%d\n",ans); } int main() { int tc;get1(tc); while(tc--)solve(); return 0; }