#P1227A. Math Problem
Math Problem
No submission language available for this problem.
Description
Your math teacher gave you the following problem:
There are segments on the -axis, . The segment includes the bounds, i.e. it is a set of such that . The length of the segment is equal to .
Two segments and have a common point (intersect) if there exists that and . For example, and have a common point, but and don't have.
You should add one segment, which has at least one common point with each of the given segments and as short as possible (i.e. has minimal length). The required segment can degenerate to be a point (i.e a segment with length zero). The added segment may or may not be among the given segments.
In other words, you need to find a segment , such that and every have a common point for each , and is minimal.
The first line contains integer number () — the number of test cases in the input. Then test cases follow.
The first line of each test case contains one integer () — the number of segments. The following lines contain segment descriptions: the -th of them contains two integers ().
The sum of all values over all the test cases in the input doesn't exceed .
For each test case, output one integer — the smallest possible length of the segment which has at least one common point with all given segments.
Input
The first line contains integer number () — the number of test cases in the input. Then test cases follow.
The first line of each test case contains one integer () — the number of segments. The following lines contain segment descriptions: the -th of them contains two integers ().
The sum of all values over all the test cases in the input doesn't exceed .
Output
For each test case, output one integer — the smallest possible length of the segment which has at least one common point with all given segments.
Samples
Note
In the first test case of the example, we can choose the segment as the answer. It is the shortest segment that has at least one common point with all given segments.