This page is still under construction.

Parts of this page are still being built. What you see may change.

Airplane Parking

Interview

Time limit1sMemory limit128 MB

Summary
Given N time intervals (arrival, departure), find the largest subset that can be scheduled in a stack, so planes leave in last-in first-out order.
Level

Medium7 of 10

Topics
Dynamic programming, Intervals, Sorting, Stack
Solved
No attempts yet

Problem

Jack has started a new business: a parking lot for airplanes. He bought a large but very narrow strip of land, so airplanes can only enter and leave in Last-In First-Out (LIFO) order; the lot behaves exactly like a stack (see the picture below). There is no way to pull an airplane out from the back to let others move, so the airplane parked most recently must always be the first to leave.

parking lot

Because of this restriction it is not always possible to satisfy every parking request. Each request consists of a planned arrival time and a planned departure time. The table below shows the requests of 4 airplanes.

AirplaneArrivalDeparture
1110
225
337
469

Here airplanes 1, 2, and 4 can be accepted together, but airplanes 2 and 3 cannot both be accepted.

Different airplanes may share the same planned arrival time or the same planned departure time. Jack's crew is highly skilled: whenever a valid parking order exists, they will find it. Consider another example.

AirplaneArrivalDeparture
51012
61015
71317

Although airplanes 5 and 6 arrive at the same time, the crew knows airplane 5 must leave before airplane 6, so they park airplane 6 first and airplane 5 on top of it.

Given a list of parking requests, determine the maximum number of airplanes that can be parked, given that airplanes may only leave in Last-In First-Out order.

Input

The first line contains an integer TT, the number of test cases (1≤T≤51 \le T \le 5). Each test case has the following format.

The first line contains an integer NN (1≤N≤3001 \le N \le 300), the number of airplanes. Each of the next NN lines contains two integers SiS_i and TiT_i (0≤Si<Ti≤1090 \le S_i < T_i \le 10^9): the planned arrival time and planned departure time of airplane ii.

Output

For each test case, print a single line containing one integer: the maximum number of airplanes that can be parked in Jack's parking lot.

Examples1

  1. Example 1

    Input
    2
    4
    1 10
    2 5
    3 7
    6 9
    3
    10 12
    10 15
    13 17
    
    Expected output
    3
    2