This page is still under construction.

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

History class

Time limit10sMemory limit128 MB

Summary
Find the event order that respects disjoint time order and minimizes the largest position gap between overlapping intervals.
Level

Hard8 of 10

Topics
Intervals, Topological sort, Binary search, Greedy
Solved
No attempts yet

Problem

Hyunsoo is a professor who teaches Korean history at Sogang University. He has nn historical events to cover, one per class period, so he has to decide which event belongs in which class.

Event ii happened during the interval [ai,bi][a_i, b_i]. Two events are related when their intervals share at least one point. Students understand related events better when the classes covering them sit close together. Two events that are not related must be taught in the order they happened: if A and B are not related and A happened before B, then A has to be taught before B.

The distance between class ii and class jj is ∣i−j∣|i - j|. Fix one order of the classes and let kk be the largest distance between two related events. Write a program that finds the smallest kk over all orders obeying the rule above. If no two events are related, kk is 00.

Input

The first line contains the number of test cases TT.

The first line of each test case contains the number of events nn (1≤n≤500001 \le n \le 50000). Each of the next nn lines contains the two endpoints aia_i and bib_i of the interval of one event (−109≤ai≤bi≤109-10^9 \le a_i \le b_i \le 10^9). No two events have the same interval.

Output

For each test case, print the smallest kk on its own line.

Examples3

  1. Example 1

    Input
    1
    3
    1 6
    2 3
    4 5
    
    Expected output
    1
    
  2. Example 2

    Input
    3
    1
    5 5
    2
    0 1
    2 3
    2
    0 5
    3 7
    
    Expected output
    0
    0
    1
    
  3. Example 3

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