#include #include #include using namespace std; #define ll __int64 ll head[200005]; struct EdgeNode { ll to; ll w; ll next; }e[200005*20]; ll vis[200005]; ll dis[200005]; ll Id[200005]; ll n,m,cont; ll ans; void add(ll from,ll to,ll w) { e[cont].to=to; e[cont].next=head[from]; e[cont].w=w; head[from]=cont++; } void SPFA(ll ss) { memset(vis,0,sizeof(vis)); queues; for(ll i=1;i<=n;i++)dis[i]=1000000000000000000ll; for(ll i=1;i<=n;i++)Id[i]=1000000000000000000ll; Id[ss]=0; dis[ss]=0; s.push(ss); while(!s.empty()) { ll u=s.front(); //printf("%d %d\n",ss,u); s.pop();vis[u]=0; for(ll k=head[u];k!=-1;k=e[k].next) { ll v=e[k].to; if(dis[v]>dis[u]+e[k].w) { Id[v]=max(Id[u],u); if(dis[u]==0)Id[v]=0; dis[v]=dis[u]+e[k].w; if(vis[v]==0) { vis[v]=1; s.push(v); } } else if(dis[v]==dis[u]+e[k].w) { if(Id[v]>Id[u]) { //printf("yes\n"); Id[v]=min(Id[v],max(Id[u],u)); if(dis[u]==0)Id[v]=0; if(vis[v]==0) { vis[v]=1; s.push(v); } } } } } //printf("------------%d\n",ss); for(ll i=1;i<=n;i++) { if(dis[i]==1000000000000000000ll)continue; //printf("%I64d ",Id[i]); if(Id[i]==ss)continue; ans=ans+Id[i]; ans=ans%998244353 ; } //printf("\n"); } int main() { ll t;scanf("%I64d",&t); while(t--) { ans=0; cont=0; memset(head,-1,sizeof(head)); scanf("%I64d%I64d",&n,&m); for(ll i=1;i<=m;i++) { ll u,v,w; scanf("%I64d%I64d%I64d",&u,&v,&w); add(u,v,w); add(v,u,w); } for(ll i=1;i<=n;i++) { SPFA(i); } printf("%I64d\n",ans); } }