Public Judge

pjudge

时间限制: 2 s 内存限制: 512 MB 总分: 7 可 Hack ✓
统计

While attending the Pre-SDOI Training Camp (also known as "Public Easy Round #2") hosted by country P, you encountered the following problem:

Given a directed graph $G$ with $n$ vertices and $m$ edges, it is guaranteed that its underlying undirected graph $G'$ is a cactus and contains no multiple edges or self-loops. You need to find the number of ordered pairs $(x, y)$ such that there exists a path from $x$ to $y$ in $G$. Specifically, $(x, x)$ must also be counted in the answer.

Definition of a cactus: A connected simple undirected graph where every edge belongs to at most one simple cycle.

Definition of an underlying graph: For a directed graph $G=(V, E)$, define a new undirected graph $G'=(V, E')$, where $E'$ is the set of edges obtained by replacing every directed edge in $E$ with an undirected edge. $G'$ is called the underlying graph of $G$.

Input

The first line contains a positive integer $T$, representing the number of test cases. For each test case:

  • The first line contains two positive integers $n$ and $m$.
  • The next $m$ lines each contain two positive integers $u$ and $v$, representing a directed edge from $u$ to $v$.

Output

For each test case:

  • A non-negative integer representing the answer.

Examples

Input 1

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

Output 1

6
18

Input 2

See provided files.

Constraints

For all test cases, it is guaranteed that $1 \le T \le 10^5$, $2 \le n \le 2.5\times 10^5$, $n-1 \le m \le \left\lfloor \dfrac{3(n-1)}{2}\right\rfloor$, and $\sum n \le 2.5\times 10^5$.

Subtask ID Additional Constraints Score
$1$ $n \leq 8, m \leq 10$ $1$
$2$ $6$

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.