This page is still under construction.

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

Moving Tables

Interview

Time limit1sMemory limit128 MB

Summary
Each move occupies a corridor segment; find the minimum number of 10-minute rounds so that overlapping segments never share a round.
Level

Medium6 of 10

Topics
Greedy, Sorting, Intervals
Solved
No attempts yet

Problem

A company occupies an entire floor of a building laid out as shown below. The rooms on this floor are numbered as in the figure.

movingtable.png

There are 200 rooms on each side of the corridor, 400 rooms in total. To remodel several of them, the company must move many tables from one room to another. The corridor is narrow, so only one table can pass through it at a time.

Moving a single table from one room to another takes 10 minutes. While a table is moved from room ii to room jj, the stretch of corridor from in front of room ii to in front of room jj is in use. During the same 10-minute slot, jobs whose corridor stretches do not overlap can be carried out at the same time.

For example, moving a table from room 30 to room 50 and moving one from room 60 to room 90 use disjoint stretches of corridor, so they can be done simultaneously. Moving from room 11 to room 12 and from room 14 to room 13 also do not overlap, so they can be done together.

On the other hand, moving from room 20 to room 40 and moving from room 31 to room 80 both use the corridor from in front of room 31 to in front of room 40, so they cannot be done at the same time. Likewise, moving from room 1 to room 4 and moving from room 3 to room 6 both need the corridor in front of room 3, so they cannot be done together.

Each room has at most one table entering or leaving it. Compute the minimum time needed to move all the tables.

Input

The first line contains the number of test cases TT.

For each test case, the first line contains the number of moves NN (1≤N≤2001 \le N \le 200). Each of the following NN lines contains two positive integers ss and tt, meaning that a table is moved from room ss to room tt. Room numbers range from 1 to 400, and within a single test case each room number appears at most once.

Output

For each test case, print on its own line the minimum time, in minutes, needed to finish moving all the tables.

Examples1

  1. Example 1

    Input
    3
    4
    10 20
    30 40
    50 60
    70 80
    2
    1 3
    2 200
    3
    10 100
    20 80
    30 50
    
    Expected output
    10
    20
    30