Under construction

Still being built. Things may change

Wooksin-ness of a graph

Interview

Time limit1sMemory limit1024 MB

Summary
Given undirected graphs, find the fewest edges to add so the graph contains a cycle, or print -1 if that is impossible.
Level

Easy3 of 10

Topics
Graph, Union-find, Implementation
Solved
No attempts yet

Problem

Problem

At the headquarters of algospot.com, Toivoa "the chairman" kept pestering Astein "the slave" for a new graph problem for a long time. So Astein came up with the following problem.

The Wooksin-ness of an undirected graph G(V,E)G(V, E) is the minimum number of additional edges needed for the graph to contain a cycle. Formally:

min⁡∣E′−E∣s.t.E′⊇E and G′(V,E′) has at least one cycle\min |E' - E| \quad \text{s.t.} \quad E' \supseteq E \text{ and } G'(V, E') \text{ has at least one cycle}

Loops (edges that start and end at the same vertex) and multiple edges between the same pair of vertices are not allowed.

Write a program that calculates the Wooksin-ness of a given graph.

Input

Input

The input consists of TT test cases. The first line contains the number of test cases TT.

The first line of each test case contains two integers VV and EE (1≤V≤1001 \le V \le 100, 0≤E≤10000 \le E \le 1000), where VV is the number of vertices and EE is the number of edges. The vertices are numbered from 00 to V−1V - 1. Each of the following EE lines contains two integers, the numbers of the two vertices joined by an edge.

The input contains no loops. There is at most one edge between any pair of vertices.

Output

Output

For each test case, print one line containing the minimum number of edges that must be added to the graph so that it contains a cycle. If this is impossible, print −1-1.

Examples1

  1. Example 1

    Input
    2
    3 2
    0 1
    2 0
    3 3
    0 1
    1 2
    2 0
    
    Expected output
    1
    0