Under construction

Still being built. Things may change

New Employees

Interview

Time limit2sMemory limit256 MB

Summary
Given N applicants ranked by two criteria, count applicants not dominated in both ranks by any other applicant (classic sort plus max-suffix pattern).
Level

Medium4 of 10

Topics
Sorting, Greedy
Solved
No attempts yet

Problem

A company is hiring new employees. Each applicant has two ranks: a document-screening rank and an interview rank.

The company does not hire an applicant if another applicant is better in both ranks. In other words, if there exists an applicant B whose document-screening rank and interview rank are both better than applicant A's, then A cannot be hired.

Find the maximum number of new employees the company can hire while following this rule.

Input

The first line contains the number of test cases T (1 <= T <= 20).

For each test case, the first line contains the number of applicants N (1 <= N <= 100,000). Each of the next N lines contains two integers: an applicant's document-screening rank and interview rank.

Each rank is a permutation of the integers from 1 through N with no ties. A smaller rank is better.

Output

For each test case, print the maximum number of new employees that can be hired on its own line.

Examples1

  1. Example 1

    Input
    2
    5
    3 2
    1 4
    4 1
    2 3
    5 5
    7
    3 6
    7 3
    4 2
    1 4
    5 7
    2 5
    6 1
    
    Expected output
    4
    3