A fair competition

2.5

30 votes
DSU, Basics of Disjoint Data Structures, DFS, BFS, Data Structures, Disjoint Data Structures
Problem

In competition, N participants are competing against each other for the top two spots. There are M friendly relations between participants. In each friendship relation, you will be given two integers L and R, indicating L and R are friends.

Note

  • If A and B are friends, B and C are friends, then A, B, C will have the same friend circle.
  • Two combinations are considered different if either first or second ranker is different.
  • The same friendship relation can occur multiple times in input, however, L will never be equal to R.

Now, the jury has to decide the winners for the top two spots but he does not want to select both participants from the same friend circle so the competition seems fairer. Find the number of ways in which he can do so.

Input format

  • The first line contains T denoting the number of test cases. 
  • The first line of each test case contains two space-separated integers N and M denoting the number of participants and the number of friendly relations.
  • Next M lines of each test case contain two integers L and R indicating a friendly relation between L and R.

Output format

For each test case, print a single line denoting the number of ways in which the jury can choose the top two participants.

Constraints

1T20000

2N200000

1M500000

1L,RN

Sum of N over all test cases does not exceed 200000 and M will not exceed 500000

 

Sample Input
2
3 1
1 3
4 2
1 2
3 4
Sample Output
4
8
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

1 and 3 are form a friend circle.

Following are the 4 possible ways: (1,2),(2,1),(1,3) and (3,1).

Editor Image

?