Under construction

Still being built. Things may change

Handing Out Books

Interview

Time limit2sMemory limit256 MB

Summary
Assign each applicant at most one distinct book numbered within their requested interval to maximize the number of satisfied applicants.
Level

Medium4 of 10

Topics
Greedy, Intervals, Sorting
Solved
No attempts yet

Problem

Jiho is tidying up his room and decided to give away the textbooks he no longer needs. There are NN books to hand out, and he labeled them with the distinct numbers 11 through NN so he can tell them apart.

MM undergraduates at Sogang University applied for a book. Jiho asked each of them to write two integers aa and bb (1≤a≤b≤N1 \le a \le b \le N) on an application form. He then picks one book that is still left and whose number is between aa and bb, and gives it to that student. If books aa through bb have all gone to other students already, that student receives nothing.

Jiho can process the applications in any order and can pick any allowed book each time. Find the largest number of students who receive a book.

Input

The first line contains the number of test cases TT.

The first line of each test case contains the integers NN (1≤N≤10001 \le N \le 1000) and MM (1≤M≤10001 \le M \le 1000), separated by a space. Each of the next MM lines contains the two integers aia_i and bib_i (1≤ai≤bi≤N1 \le a_i \le b_i \le N) written on one application form.

Output

For each test case, print the largest number of students who receive a book, one per line.

Examples1

  1. Example 1

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