HRY and balls
题号:NC25398
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

When HRY was young, he like playing balls. The young boy was tidy, so on each ball he wrote a number i which means the ball belongs to box i. When playing balls, he put balls into these boxes randomly, but before bedtime, he will put them back to the boxes where they belong to.

As HRY is a little child, the way he put balls back is simple. Every time he chooses a box, pick out all the balls in the box, put every ball into the box it should be. Then he chooses another box, and repeat this operation until every ball is in the right box. The time he need for each box is the number of balls in this box now, and for any other operations the time is ignored.

As an expert of playing balls, HRY found that the order of choosing boxes greatly influenced the total time to put the balls back. But HRY is still too young, so he wants you, a JBer, to calculate how much time he needs at least to put the balls back.

输入描述:

The first line contains an integer T, indicating the number of test cases. 
For each test case the first line contains two integers n, m, indicating the number of balls and the number of boxes respectively.
Then follows n lines. The ith line contains two integers a_i and b_i, which means that ball i is currently in box a_i and should be put into box b_i.

The sum of n does not exceed , and no more than 5 test cases has .

输出描述:

For each test case output a line of a integer, indicating the least total time needed.
示例1

输入

复制
3
2 2
1 2
2 1
2 2
1 1
1 2
2 2
1 1
2 2

输出

复制
3
2
0