QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 512 MB مجموع النقاط: 100

#14269. Dish Preparation

الإحصائيات

The famous gourmet Little A has been invited to the ATM Hotel to evaluate their dishes.

The ATM Hotel has prepared $N$ dishes for Little A, numbered from $1$ to $N$ in descending order of their estimated quality, where the dish with the highest estimated quality is numbered $1$. Due to flavor pairing constraints, some dishes must be prepared before others. Specifically, there are $M$ constraints of the form "dish $i$ must be prepared before dish $j$", which we denote as $\langle i, j \rangle$. The hotel wants to find an optimal preparation order such that Little A can eat the highest-quality dishes as early as possible. That is:

(1) Subject to all constraints, dish $1$ should be prepared as early as possible; (2) Subject to all constraints and the condition that dish $1$ is prepared as early as possible, dish $2$ should be prepared as early as possible; (3) Subject to all constraints and the conditions that dishes $1$ and $2$ are prepared as early as possible, dish $3$ should be prepared as early as possible; (4) Subject to all constraints and the conditions that dishes $1, 2,$ and $3$ are prepared as early as possible, dish $4$ should be prepared as early as possible; (5) And so on.

Example 1: There are $4$ dishes and two constraints $\langle 3, 1 \rangle$ and $\langle 4, 1 \rangle$. The preparation order is $3, 4, 1, 2$. Example 2: There are $5$ dishes and two constraints $\langle 5, 2 \rangle$ and $\langle 4, 3 \rangle$. The preparation order is $1, 5, 2, 4, 3$.

In Example 1, we first consider $1$. Because of the constraints $\langle 3, 1 \rangle$ and $\langle 4, 1 \rangle$, we must prepare $3$ and $4$ before $1$. According to condition (3), dish $3$ should be prioritized over $4$, so the first three dishes in the order are $3, 4, 1$. Next, we consider $2$, resulting in the final order $3, 4, 1, 2$.

In Example 2, preparing $1$ first does not violate any constraints. Next, considering $2$, we have the constraint $\langle 5, 2 \rangle$, so we prepare $5$ then $2$. Next, considering $3$, we have the constraint $\langle 4, 3 \rangle$, so we prepare $4$ then $3$. The final order is $1, 5, 2, 4, 3$.

You need to find this optimal preparation order. If no such order exists, output "Impossible!".

Input

The first line contains a positive integer $D$, representing the number of test cases. Each test case consists of: The first line contains two space-separated positive integers $N$ and $M$, representing the number of dishes and the number of constraints, respectively. The next $M$ lines each contain two positive integers $x$ and $y$, representing the constraint "dish $x$ must be prepared before dish $y$". (Note: There may be duplicate constraints among the $M$ constraints.)

Output

The output contains $D$ lines. Each line contains $N$ integers representing the optimal preparation order, or "Impossible!" if no solution exists.

Examples

Input 1

3
5 4
5 4
5 3
4 2
3 2
3 3
1 2
2 3
3 1
5 2
5 2
4 3

Output 1

1 5 3 4 2
Impossible!
1 5 2 4 3

Note

In the second test case, the requirements are that dish $1$ must be prepared before $2$, $2$ before $3$, and $3$ before $1$. This is impossible to satisfy, leading to no solution.

Constraints

  • $30\%$ of the data: $N, M \le 200, D \le 3$
  • $70\%$ of the data: $N, M \le 5000, D \le 3$
  • $100\%$ of the data: $N, M \le 100000, D \le 3$

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.