P18100 [CCPC 2026 Changchun] A Surprise for a Friend

Description

Little $\varepsilon$ and Little $\omega$ are two girls who cherish the time they spend together. Little $\omega$ is preparing a gift for Little $\varepsilon$ and records it as a secret sequence $a$, all of whose elements are non-negative integers not exceeding $2^k-1$. Curious Little $\varepsilon$ wants to learn about this surprise in advance, so she asks Little $\omega$ a total of $m$ questions, where the $i$-th question asks for the bitwise AND of $a_{x_i}$ and $a_{y_i}$. Little $\omega$ tells Little $\varepsilon$ the answer to every question, where the answer to the $i$-th question is $z_i$. That is, Little $\omega$ tells Little $\varepsilon$ that the sequence $a$ satisfies $a_{x_i}\operatorname*{AND}a_{y_i}=z_i$, where $x\operatorname*{AND}y$ denotes the bitwise AND of $x$ and $y$. Little $\varepsilon$ feels that Little $\omega$ is teasing her and suspects that Little $\omega$ is just answering at random, and that a sequence $a$ satisfying the above conditions may not exist at all! So she comes to you, and you need to tell Little $\varepsilon$ whether a sequence $a$ satisfying the above conditions exists. Furthermore, Little $\varepsilon$ also wants to know, if such a sequence $a$ exists, what is the minimum possible sum of all its elements.

Input Format

The first line contains one integer $T$ ($1\le T\le 5\times 10^5$), representing the number of test cases. For each test case: - The first line contains three integers $n,m,k$ ($1\le n\le 200000$, $0\le m\le 200000$, $1\le k\le 30$), representing the length of the sequence $a$, the number of questions and the upper bound on the binary length of the elements of $a$. - The next $m$ lines, each containing three integers $x_i,y_i,z_i$ ($1\le x_i,y_i\le n$, $0\le z_i

Output Format

For each test case, if no valid $a$ exists, output `-1`; otherwise output a single line with one non-negative integer, representing the minimum possible sum of all elements of the sequence $a$.

Explanation/Hint

For the first test case, $a$ has length $3$ and every element of it is $0$ or $1$, and $a_1\operatorname*{AND}a_2=0$, $a_1\operatorname*{AND}a_3=0$, $a_2\operatorname*{AND}a_3=1$. Since $a_2\operatorname*{AND}a_3=1$, we have $a_2=1,a_3=1$; and from $a_1\operatorname*{AND}a_2=0$ we know $a_1=0$. Hence $a_1=0,a_2=1,a_3=1$, and one can verify that this sequence satisfies all conditions. In this case the sum of all elements of $a$ is $2$. For the second test case, $z_1=z_2=2^{30}-1$, so $a_1=a_2=a_3=2^{30}-1$ is determined uniquely, and the sum of the elements of $a$ is $3\times(2^{30}-1)$. For the third test case, $a_1\operatorname*{AND}a_2=1$, $a_1\operatorname*{AND}a_3=1$, $a_2\operatorname*{AND}a_3=0$. Since $a_1\operatorname*{AND}a_2=1$, we have $a_1=1,a_2=1$; and from $a_1\operatorname*{AND}a_3=1$ we know $a_3=1$, which contradicts the conditions, so no such $a$ exists. For the fourth test case, $a_1\operatorname*{AND}a_1=4514$, so $a_1=4514$, and the sum of the elements of $a$ is minimized when all the other $a_i$ ($2\le i\le n$) are $0$, which gives $4514$. For the fifth test case, since $a_1\operatorname*{AND}a_1=5$, we have $a_1=5$; similarly $a_2=6$. One can verify that in this case $a_3$ is $5$ or $7$, and when $a_3=5$ the sum of all elements of $a$ is $16$, which can be shown to be the minimum possible value. Translated by QwenWork