eolymp
bolt
Try our new interface for solving problems
Problems

Connecting Two Barns

Connecting Two Barns

Farmer John's farm consists of a set of n fields, conveniently numbered 1..n. Between these fields are m bidirected paths, each connecting a pair of fields.

The farm contains two barns, one in field 1 and the other in field n. Farmer John would like to ensure that there is a way to walk between the two barns along some series of paths. He is willing to build up to two new paths to accomplish this goal. Due to the way the fields are situated, the cost of building a new path between fields i and j is (ij) ^ 2.

Please help Farmer John determine the minimum cost needed such that barns 1 and n become reachable from each-other.

Input

The first line contains the number of test cases t (1t20).

Each sub-test case starts with two integers n (1n105) and m (0m105). Next, m lines follow, each one containing two integers i and j, indicating a path between two different fields i and j. It is guaranteed that there is at most one path between any two fields, and that the sum of n + m over all sub-test cases is at most 5 * 105.

Output

Output t lines. The i-th line should contain a single integer giving the minimum cost for the ith sub-test case.

Example

In the first test case, it is optimal to connect fields 2 and 3 with a path, and fields 3 and 4 with a path.

In the second test case, it is optimal to connect fields 3 and 4 with a path. No second path is needed.

Time limit 1 second
Memory limit 128 MiB
Input example #1
2
5 2
1 2
4 5
5 3
1 2
2 3
4 5
Output example #1
2
1
Source 2021 USACO December, Silver