SQDANCE - Square dance

no tags 

You are hired by French NSA to break the RSA code used on the Pink Card. The easiest way to do that is to factor the public modulus and you have found the fastest algorithm to do that, except that you have to solve a subproblem that can be modeled in the following way.

Let P = {p1, p2 ... pn} be a set of prime numbers. If S = {s1, s2... su} and T = {t1 ... tv} are formed with elements of P, then S*T will denote the quantity s1 * s2 * ... * su * t1 * t1 * ... tv.

We call relation a set of two primes p, q, where p and q are distinct elements of P. You dispose of a collection of R relations Si = {pi, qi} and you are interested in finding sequences of these Si1, Si2 ... Sik such that Si1 * Si2 * ... * Sik is a perfect square.

The way you look for these squares is the following. The ultimate goal is to count squares that appear in the process. Relations arrive one at a time. You maintain a collection of C relations that do not contain any square subproduct. This is easy: at first, C is empty. Then a relation arrives and C begins to grow. Suppose a new relation {p, q} arrives. If no square appears when adding {p, q} to C, then {p, q} is added to the collection. Otherwise, a square is about to appear, we increase the number of squares, but we do not store this relation, hence C keeps the desired property.

Let us consider an example. First arrives S1 = {2, 3} and we put it in C. Then arrives S2 = {5, 11}, S3 = {3, 7} and they are stored in C. Now enters the relation S4 = {2, 7}. This relation could be used to form the square: S1 * S3 * S4 = (2 * 3) * (3 * 7) * (2 * 7) = (2 * 3 * 7)2.

So we count 1 and do not store S4 in C. Now we consider S5 = {5, 11} that could make a square with S2, so we count 1 square more. Then S6 = {2, 13} is put into C. Now C could make the square S1 * S3 * S6 * S7. Eventually, we get 3 squares.

Input

The first line of the input contains a number T ≤ 30 that indicates the number of test cases to follow. Each test case begins with a line containing two integers P and R: P ≤ 105 is the number of primes occurring in the test case; R (≤ 105) is the number of sets of primes that arrive. The subsequent R lines each contain two integers i and j making a set {pi,qi} (1 ≤ i, j ≤ P). Note that we actually do not deal with the primes, they are irrelevant to the solution.

Output

For each test case, output the number of squares that can be formed using the preceding rules.

Example

Input:
2
6 7
1 2
3 5
2 4
1 4
3 5
1 6
4 6
2 3
1 2
1 2
1 2

Output:
3
2
Warning: large Input/Output data, be careful with certain languages

hide comments
jarircse16: 2023-06-28 19:30:01

Now C could make the square S1 * S3 * S6 * S7. but S7 is missing in the statement! S7 = {7,13}.

Last edit: 2023-06-28 19:31:21
Deepak Gupta: 2014-11-29 22:05:25

These two points should have been mentioned clearly:

Treat every number just like a prime.
If both numbers are ==1 do not increment the ans.

Last edit: 2014-11-29 22:09:34

Added by:Adrian Kuegel
Date:2004-07-13
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:ACM Southwestern European Regional Contest, Paris 2003