Codeforces Round 990 (Div. 1)
E2
Cheops and a Contest (Hard Version)
Codeforces
3500
原题
← E1F1 →
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, $m$ is arbitrary. You can hack only if you solved all versions of this problem.

翻译中…

There is a problem-solving competition in Ancient Egypt with $n$ participants, numbered from $1$ to $n$. Each participant comes from a certain city; the cities are numbered from $1$ to $m$. There is at least one participant from each city.

翻译中…

The $i$-th participant has strength $a_i$, specialization $s_i$, and wisdom $b_i$, so that $b_i \ge a_i$. Each problem in the competition will have a difficulty $d$ and a unique topic $t$. The $i$-th participant will solve the problem if

翻译中…
  • $a_i \ge d$, i.e., their strength is not less than the problem's difficulty, or
  • $s_i = t$, and $b_i \ge d$, i.e., their specialization matches the problem's topic, and their wisdom is not less than the problem's difficulty.
翻译中…

Cheops wants to choose the problems in such a way that each participant from city $i$ will solve strictly more problems than each participant from city $j$, for all $i \lt j$.

翻译中…

Please find a set of at most $5n$ problems, where the topics of all problems are distinct, so that Cheops' will is satisfied, or state that it is impossible.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$, $m$ ($2 \le m \le n \le 3 \cdot {10}^5$) — the number of participants and the number of cities.

翻译中…

The following $n$ lines describe the participants. The $i$-th line contains three integers —$a_i$, $b_i$, $s_i$ ($0 \le a_i, b_i, s_i \le {10}^9$, $a_i \le b_i$) — strength, wisdom, and specialization of the $i$-th participant, respectively.

翻译中…

The next $m$ lines describe the cities. In the $i$-th line, the first number is an integer $k_i$ ($1 \le k_i \le n$) — the number of participants from the $i$-th city. It is followed by $k_i$ integers $q_{i, 1}, q_{i, 2}, \ldots, q_{i, k_i}$ — ($1 \le q_{i, j} \le n$, $1 \le j \le k_i$) — the indices of the participants from this city. It is guaranteed that each participant is mentioned exactly once.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, if there exists a set of problems that satisfies Cheops' conditions, then in the first line output a single integer $p$ ($1 \le p \le 5n$) — the number of problems in your solution.

翻译中…

Then output $p$ lines, each containing two integers $d$ and $t$ ($0 \le d, t \le {10}^9$) — the difficulty and topic of the respective problem. The topics must be distinct.

翻译中…

If there is no set of problems that meets Cheops' wishes, print $-1$ instead.

翻译中…

样例

Input #1
2
5 2
5 7 1
6 7 2
3 9 2
5 10 3
4 4 1
2 1 2
3 3 4 5
2 2
1 2 1
1 2 1
1 2
1 1
Output #1
7
6 4
6 5
5 6
5 7
4 8
4 9
7 1
-1
InputCopy OutputCopy
题目信息
题号
2046E2
时间限制
4 s
内存限制
512 MB
标签
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。