#include #include #include #include #include #include #include using namespace std; #define ll long long #include const double eps=1e-8; const ll inf=1e9; const ll mod=998244353; const int maxn=1e3+10; struct node { int d; ll len; node *to; }*e[maxn]; struct rec { int d,maxd; ll dist; bool operator<(const rec &y) const { return dist>y.dist; } }; priority_queue > st; ll dist[maxn]; int maxp[maxn]; int main() { node *p; int t,n,m,u,v,i,j,be,dd; ll sum,w; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); for (i=1;i<=n;i++) e[i]=0; while (m--) { scanf("%d%d%I64d",&u,&v,&w); p=new node(); p->d=v; p->len=w; p->to=e[u]; e[u]=p; p=new node(); p->d=u; p->len=w; p->to=e[v]; e[v]=p; } sum=0; for (be=1;be<=n;be++) { memset(dist,0x7f,sizeof(dist)); dist[be]=0; memset(maxp,0,sizeof(maxp)); while (!st.empty()) st.pop(); st.push({be,0,0}); while (!st.empty()) { u=st.top().d; v=st.top().maxd; w=st.top().dist; st.pop(); if (w!=dist[u] || v!=maxp[u]) continue; if (u!=be) v=max(v,u); p=e[u]; while (p) { dd=p->d; if (dist[dd]>w+p->len || (dist[dd]==w+p->len && maxp[dd]>v)) { dist[dd]=w+p->len; maxp[dd]=v; st.push({dd,v,dist[dd]}); } p=p->to; } } for (i=1;i<=n;i++) sum+=maxp[i]; } printf("%I64d\n",sum%mod); } return 0; } /* 2 4 2 1 2 3 2 3 4 4 4 1 2 1 2 3 1 3 4 1 4 1 1 */