The Matrix
题号:NC237450
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

The Matrix can be regarded as an enormous connected weighted graph. To describe the Matrix, you are given k connected weighted graphs . The graphs will not contain self loops or duplicate edges. The Matrix is the graph (V,E) where . That is, any vertex in V can be described as a tuple where for .

For two vertices and in the Matrix, there is an edge between the vertices if and only if there exists an i such that (v_i,v'_i) is in E_i and for all we have that . As there is no self loops in the k graphs it is obvious that such i will be unique. The weight of the edge will be the weight of the edge (v_i,v'_i) is in E_i.

You are required to output weight of the minimum spanning tree of (V,E). As the weight could be very large, you are only required to output modulo 998,244,353.

输入描述:

The first line of the input contains one integer k ().
Then the description of the k graphs follows. For each graph, the first line contains two integers n,m (). The i-th of the following m lines contains three integers v_i,u_i,w_i (), indicating an edge of the graph.
The sum of n and m over all graphs will be no greater than 1000000, respectively.

输出描述:

Output one integer, the answer.
示例1

输入

复制
2
2 1
1 2 4
3 3
1 2 1
1 3 2
2 3 4

输出

复制
10
示例2

输入

复制
3
2 1
1 2 4
2 1
1 2 3
2 1
1 2 1

输出

复制
14