Centipede legs

Given n and m notes, choose left and right leg counts summing to n, both at least 1, maximizing how many notes have l_i <= left and r_i <= right, breaking ties by smallest left count.

Medium5MathPrefix sumBinary searchSortingInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

When Phil was a boy he kept a centipede. Centipedes have many legs. Phil's centipede had nn legs in total: some of them were left legs and the rest were right legs. Every day the centipede used some of its legs, and it always used at least one left leg and at least one right leg.

Each day Phil wrote down in his notebook how many left legs and how many right legs the centipede used that day.

Phil now wants to recover from his notes how many left legs and how many right legs his centipede had. Phil was not accurate, so some notes may be wrong. A note li,ril_i, r_i is correct if the centipede had at least lil_i left legs and at least rir_i right legs.

Choose a number of left legs and a number of right legs that makes the number of correct notes as large as possible. Each of the two counts is at least 1, and together they add up to nn.

Input

The input contains several test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4).

The first line of each test case contains the total number of centipede legs nn (2n1092 \le n \le 10^9). The next line contains the number of Phil's notes mm (1m1051 \le m \le 10^5).

Each of the following mm lines contains two integers lil_i and rir_i, the number of left legs and the number of right legs the centipede used on day ii according to Phil's notes (1li,rin1 \le l_i, r_i \le n).

The total number of notes over all test cases in one input does not exceed 10510^5.

Output

For each test case print one line with two integers: the number of left legs and the number of right legs that make the number of correct notes as large as possible.

If several answers reach that maximum, print the one with the smallest number of left legs.