Universal Cup Judging System

Universal Cup

시간 제한: 2.5 s 메모리 제한: 1024 MB 총점: 100 난이도: [표시] 해킹 가능 ✓
통계

Given an undirected graph with $n$ vertices, initially the graph has no edges. Maintain $q$ operations of the following two types:

  • $+\ u\ v\ w$: Add an edge connecting vertices $u$ and $v$, with a weight of $w$.
  • $?\ u\ v$: Define the weight of a path to be the bitwise-and of the weights of all edges in the path. Calculate the largest possible weight of a path starting from vertex $u$ and ending at vertex $v$. If there is no path connecting vertices $u$ and $v$, the answer should be $-1$.

Input

There are multiple test cases. The first line of the input contains an integer $T$ ($1 \le T \le 100$) indicating the number of test cases. For each test case:

The first line contains two integers $n$ and $q$ ($2 \le n \le 10^3$, $1 \le q \le 10^6$) indicating the number of vertices and the number of operations.

For the following $q$ lines, the $i$-th line first contains a character $op$ ($op \in \{+, ?\}$) indicating the type of the $i$-th query.

  • If $op = +$, then three integers $u$, $v$ and $w$ follow ($1 \le u, v \le n$, $0 \le w < 2^{12}$), indicating an operation of the first type.
  • If $op = ?$, then two integers $u$ and $v$ follow ($1 \le u, v \le n$, $u \ne v$), indicating an operation of the second type.

It's guaranteed that there is at least one operation of the second type for each test case. Also the sum of $n$ of all test cases does not exceed $10^3$, and the sum of $q$ of all test cases does not exceed $10^6$.

Output

To decrease the size of output, for each test case just output one line containing one integer, indicating the sum of the answers to all operations of the second type.

Examples

2
4 11
+ 1 3 6
+ 3 4 2
? 1 4
+ 1 4 3
? 4 1
+ 4 3 4
? 1 4
+ 3 2 3
? 1 2
+ 1 1 4
? 1 3
4 4
+ 1 2 64
+ 3 4 32
? 1 2
? 3 1
17
63

Note

For the first sample test case, the answers to the operations of the second type are $2$, $3$, $4$, $2$, and $6$.

For the second sample test case, the answers to the operations of the second type are $64$ and $-1$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.