luogu#P9343. 一曲新词酒一杯

一曲新词酒一杯

Background

Last night, I listened to songs at the pleasure house. With a pot of rough wine in hand, I looked at the bright moon by the railing. Thinking of my current situation, I did not feel lost. Instead, I was still fascinated by the joy of feasting and chanting verses. While raising my cup to the wind, I remembered a drinking-table game, and played it with my friends.

Problem Description

There are nn cups of wine on the table, numbered 1n1 \sim n. Beside the table, there are many red paper slips with the character “wine” written on them.

Next, these nn cups are operated on in order for mm operations.

There are 22 types of operations:

  • 1 x: Put 11 red slip on cup xx.
  • 2 x: Put 11 red slip on each of the other n1n-1 cups, except cup xx.

Ask: after at least how many operations will every cup have at least one red slip?

Input Format

This problem has multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case:

  • The first line contains two integers n,mn, m.
  • The next mm lines each contain two integers oi,xio_i, x_i, representing the ii-th operation.

Output Format

For each test case:

  • If after mm operations there is still at least one cup without any red slip, output one line with -1.
  • Otherwise, output one line with an integer, the answer.
2
3 3
1 1
1 2
1 3
3 2
1 1
2 2
3
-1

Hint

[Sample 1 Explanation]

For the first test case:

  • After the 11-st operation, cup 11 has 11 red slip, cup 22 has 00 red slips, and cup 33 has 00 red slips.
  • After the 22-nd operation, cup 11 has 11 red slip, cup 22 has 11 red slip, and cup 33 has 00 red slips.
  • After the 33-rd operation, cup 11 has 11 red slip, cup 22 has 11 red slip, and cup 33 has 11 red slip.

[Constraints and Notes]

This problem uses bundled tests.

  • Subtask 1 (20 points): oi=1o_i = 1.
  • Subtask 2 (20 points): oi=2o_i = 2.
  • Subtask 3 (20 points): all xix_i are equal.
  • Subtask 4 (20 points): n,m3×103\sum n, \sum m \le 3 \times 10^3.
  • Subtask 5 (20 points): no special restrictions.

For 100%100\% of the data, 1T,n,m,n,m2×1051 \le T, n, m, \sum n, \sum m \le 2 \times 10^5, oi{1,2}o_i \in \{1, 2\}, 1xin1 \le x_i \le n.

Translated by ChatGPT 5