The Matrix can be regarded as an enormous connected weighted graph. To describe the Matrix, you are given

connected weighted graphs
%2C(V_2%2CE_2)%2C%20%5Cldots%2C%20(V_k%2CE_k))
. The graphs will not contain self loops or duplicate edges. The Matrix is the graph
)
where

. That is, any vertex in

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

such that
)
is in

and for all

we have that

. As there is no self loops in the

graphs it is obvious that such

will be unique. The weight of the edge will be the weight of the edge
)
is in

.
You are required to output weight of the minimum spanning tree of
)
. As the weight could be very large, you are only required to output modulo

.