Nested Dolls

Time limit1sMemory limit128 MB

Summary
Given a set of dolls with widths and heights, partition them into the fewest strictly increasing chains in both dimensions, which equals the longest antichain by Dilworth's theorem.
Level

Medium7 of 10

Topics
Sorting, Dynamic programming, Binary search, Greedy
Solved
No attempts yet

Problem

Dilworth is the world's most prominent collector of Russian nested dolls (matryoshkas): he owns thousands of them. These are the wooden, hollow dolls of different sizes, where the smallest doll sits inside the second-smallest, which in turn sits inside the next one, and so on.

One day he wonders whether there is a different way to nest them that leaves him with fewer separate nested dolls, which would make his collection even more magnificent.

He unpacks every doll and measures the width and height of each one. A doll with width w1w_1 and height h1h_1 fits inside another doll with width w2w_2 and height h2h_2 if and only if w1<w2w_1 < w_2 and h1<h2h_1 < h_2. Each doll can directly contain at most one other doll (which may itself contain another), so the dolls in a single nested set form a chain that is strictly increasing in both width and height.

Given all the measurements, compute the smallest number of separate nested dolls (sets) into which the entire collection can be assembled.

Input

The first line contains a single integer tt (1≤t≤201 \le t \le 20), the number of test cases.

Each test case begins with a line containing a single integer mm (1≤m≤200001 \le m \le 20000), the number of dolls. It is followed by 2m2m integers w1,h1,w2,h2,…,wm,hmw_1, h_1, w_2, h_2, \ldots, w_m, h_m, where wiw_i is the width and hih_i is the height of doll ii (1≤wi,hi≤100001 \le w_i, h_i \le 10000). These 2m2m integers may be split across one or more lines and are separated by whitespace.

Output

For each test case, output a single line containing the minimum number of separate nested dolls (sets) needed to hold the entire collection.

Examples1

  1. Example 1

    Input
    4
    3
    20 30 40 50 30 40
    4
    20 30 10 10 30 20 40 50
    3
    10 30 20 20 30 10
    4
    10 10 20 30 40 50 39 51
    
    Expected output
    1
    2
    3
    2