This page is still under construction.

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

Surveillance System

Time limit1sMemory limit128 MB

Summary
Pick the fewest cameras whose clockwise ranges together cover all 100000 sectors on the circular boundary.
Level

Medium7 of 10

Topics
Greedy, Intervals, Sorting
Solved
No attempts yet

Problem

To protect the leopard of the snow covered Kilimanjaro, which is close to extinction, an international agency designated the area within 50 km of the summit of Mount Kilimanjaro as a nature reserve. The agency also built a surveillance system of closed circuit cameras to keep people out of the reserve. The system was built so that at least one camera watches every sector, and enough spare cameras run in case a camera breaks. Cameras broke far less often than expected, so the government now wants to cut maintenance costs by running fewer cameras whose ranges overlap.

The cameras sit along the boundary of the reserve, a circle of radius 50 km, and watch for people crossing it. Each camera has its own range, depending on where it is installed. The northernmost point of the boundary is sector 1, and the boundary continues clockwise as sectors 2, 3, …, 100000. Sector 100000 is followed by sector 1 again. Each camera starts at one sector and watches several consecutive sectors clockwise from there. Given the range of every camera, find the smallest number of cameras that still leaves every sector watched by at least one camera. Assume that no test case is given without an answer.

In the picture above, each arc is the range of one camera. Running only the cameras drawn in red is enough to watch every sector.

Input

Input arrives on standard input. The first line has the number of test cases TT (1≤T≤201 \le T \le 20). The first line of each test case has the number of installed cameras KK (1≤K≤200001 \le K \le 20000). Each of the next KK lines describes the range of one camera with two positive integers pp and rr (1≤p,r≤1000001 \le p, r \le 100000), which mean the rr consecutive sectors that start at sector pp and run clockwise. The numbers are separated by whitespace.

Output

Print to standard output. For each test case print the smallest number of cameras that watch every sector, one per line.

Examples3

  1. Example 1

    Input
    2
    6
    5000 12000
    15000 12000
    25000 12000
    35000 12000
    45000 12000
    55000 60000
    24
    1 11631
    11630 15322
    70349 26852
    26951 810
    27760 1097
    33356 1470
    34825 6076
    67674 2685
    41700 364
    42060 7007
    49062 2960
    6768 12678
    51922 144
    52064 42909
    94972 3314
    98285 1718
    148 315
    312 6457
    19346 14010
    38334 8335
    46664 10738
    57392 10292
    97200 2639
    99829 1173
    
    Expected output
    5
    10
    
  2. Example 2

    Input
    3
    1
    1 100000
    1
    50000 100000
    2
    1 50000
    50001 50000
    
    Expected output
    1
    1
    2
    
  3. Example 3

    Input
    3
    3
    99999 2
    100000 2
    1 99998
    6
    1 60000
    60001 50000
    10001 50000
    60001 14000
    74001 14000
    88001 12000
    2
    1 99999
    100000 1
    
    Expected output
    2
    2
    2