Math Problem
Your math teacher gave you the following problem:There are $$$n$$$ segments on the $$$x$$$-axis, $$$[l_1; r_1], [l_2; r_2], \ldots, [l_n; r_n]$$$. The segment $$$[l; r]$$$ includes the bounds, i.e. it is a set of such $$$x$$$ that $$$l \le x \le r$$$. The length of the segment $$$[l; r]$$$ is equal to $$$r - l$$$.Two segments $$$[a; b]$$$ and $$$[c; d]$$$ have a common point (intersect) if there exists $$$x$$$ that $$$a \leq x \leq b$$$ and $$$c \leq x \leq d$$$. For example, $$$[2; 5]$$$ and $$$[3; 10]$$$ have a common point, but $$$[5; 6]$$$ and $$$[1; 4]$$$ 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 $$$n$$$ segments.In other words, you need to find a segment $$$[a; b]$$$, such that $$$[a; b]$$$ and every $$$[l_i; r_i]$$$ have a common point for each $$$i$$$, and $$$b-a$$$ is minimal.
In the first test case of the example, we can choose the segment $$$[5;7]$$$ as the answer. It is the shortest segment that has at least one common point with all given segments.
The first line contains integer number $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow.The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^{5}$$$) — the number of segments. The following $$$n$$$ lines contain segment descriptions: the $$$i$$$-th of them contains two integers $$$l_i,r_i$$$ ($$$1 \le l_i \le r_i \le 10^{9}$$$).The sum of all values $$$n$$$ over all the test cases in the input doesn't exceed $$$10^5$$$.
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.
import sys
import heapq
import math
input = sys.stdin.readline
from collections import Counter, deque, defaultdict

def sieve_of_eratosthenes(n):
    sieve = [True] * (n + 1)
    (sieve[0], sieve[1]) = (False, False)
    primes = []
    for current in range(2, int(n ** 0.5) + 1):
        if sieve[current]:
            primes.append(current)
            for multiple in range(current * current, n + 1, current):
                sieve[multiple] = False
    for current in range(int(n ** 0.5) + 1, n + 1):
        if sieve[current]:
            primes.append(current)
    return primes

def inp():
    return int(input())

def inlt():
    return list(map(int, input().split()))

def insr():
    s = input()
    return list(s[:len(s) - 1])

def invr():
    return map(int, input().split())

def count_divisible_by(num, limit):
    return limit // num

def sum_range(a, b):
    return b * (b + 1) // 2 - a * (a - 1) // 2

def Yes():
    print('Yes')

def No():
    print('No')

def solve():
    n = inp()
    left = []
    right = []
    for _ in range(n):
        (l, r) = invr()
        left.append(l)
        right.append(r)
    print(max(0, max(left) - min(right)))

def main():
    for _ in range(inp()):
        solve()
main()
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    n = int(input())
    (c, d) = (0, 10 ** 9)
    for _ in range(n):
        (a, b) = map(int, input().split())
        if a > c:
            c = a
        if b < d:
            d = b
    if c <= d:
        print(0)
    else:
        print(c - d)
import sys
import math
from decimal import *

def prime_generator(nr_elemente_prime):
    vector_prime = [-1] * nr_elemente_prime
    vector_rasp = [0] * nr_elemente_prime
    vector_prime[1] = 1
    vector_rasp[1] = 1
    contor = 2
    for i in range(2, nr_elemente_prime):
        if vector_prime[i] == -1:
            vector_prime[i] = 1
            vector_rasp[contor] = i
            contor = contor + 1
        for j in range(i + i, nr_elemente_prime, i):
            if vector_prime[j] == -1:
                vector_prime[j] = i
    set_prime = set(vector_rasp)
    sume_disponibile = set()
    for i in range(3, nr_elemente_prime):
        sume_disponibile.add(vector_rasp[i - 1] + vector_rasp[i])
rasp_final = ''

def transformare_baza(numar, baza):
    transformare = ''
    while numar >= baza:
        rest = numar % baza
        numar = numar // baza
        transformare += str(rest)
    transformare += str(numar)
    noua_baza = transformare[::-1]
    return noua_baza

def bitwise_xor(a, b):
    stringul_1 = transformare_baza(a, 2)
    stringul_2 = transformare_baza(b, 2)
    lungime = max(len(stringul_1), len(stringul_2))
    raspunsul = 0
    str_answ = [0] * lungime
    for i in range(0, lungime):
        j = lungime - 1 - i
        if len(stringul_1) > i and len(stringul_2) > i:
            if stringul_1[len(stringul_1) - 1 - i] != stringul_2[len(stringul_2) - 1 - i]:
                raspunsul += 2 ** i
                str_answ[i] = '1'
        elif len(stringul_1) > i and stringul_1[len(stringul_1) - 1 - i] == '1':
            raspunsul += 2 ** i
            str_answ[i] = '1'
        elif len(stringul_2) > i and stringul_2[len(stringul_2) - 1 - i] == '1':
            raspunsul += 2 ** i
            str_answ[i] = '1'
    return raspunsul

def bitwise_or(a, b):
    stringul_1 = transformare_baza(a, 2)
    stringul_2 = transformare_baza(b, 2)
    lungime = max(len(stringul_1), len(stringul_2))
    raspunsul = 0
    str_answ = [0] * lungime
    for i in range(0, lungime):
        j = lungime - 1 - i
        if len(stringul_1) > i and len(stringul_2) > i:
            if stringul_1[len(stringul_1) - 1 - i] == '1' or stringul_2[len(stringul_2) - 1 - i] == '1':
                raspunsul += 2 ** i
                str_answ[i] = '1'
        elif len(stringul_1) > i and stringul_1[len(stringul_1) - 1 - i] == '1':
            raspunsul += 2 ** i
            str_answ[i] = '1'
        elif len(stringul_2) > i and stringul_2[len(stringul_2) - 1 - i] == '1':
            raspunsul += 2 ** i
            str_answ[i] = '1'
    return raspunsul

def suma_cifre(suma_cifrelor):
    suma_cifr = 0
    while suma_cifrelor > 0:
        suma_cifr += suma_cifrelor % 10
        suma_cifrelor = suma_cifrelor // 10
    return suma_cifr

def cmmdc(x, y):
    maximul = max(x, y)
    minimul = min(x, y)
    while maximul != minimul and minimul != 0:
        dif = maximul - minimul
        raport = dif // minimul
        maximul -= minimul * (raport + 1)
        a = max(minimul, maximul)
        b = min(minimul, maximul)
        maximul = a
        minimul = b
    return maximul
dict = {}
alfabet = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
contor = 0
k = int(input())
while k > 0:
    n = int(input())
    left_left = 10 ** 9 + 1
    left_right = 10 ** 9 + 1
    right_right = -1
    right_left = -1
    for i in range(0, n):
        (a, b) = [int(i) for i in sys.stdin.readline().split()]
        if b < left_right:
            left_right = b
            left_left = a
        if a > right_left:
            right_left = a
            right_right = b
    if right_left <= left_right:
        print(0)
    else:
        print(abs(left_right - right_left))
    k -= 1