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.


-----Input-----

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$.


-----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.


-----Example-----
Input
4
3
4 5
5 9
7 7
5
11 19
4 17
16 16
3 12
14 17
1
1 10
1
1 1

Output
2
4
0
0



-----Note-----

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.

Difficulty: EASY

Solutions:
n = int(input())
ans = []
for i in range(n):
	m = int(input())
	if m != 1:
		s = []
		for p in range(m):
			s.append(list(map(int, input().split())))
		sn = [j[0] for j in s]
		sk = [j[1] for j in s]
		a = min(sk)
		b = max(sn)
		fuckuguyspieaceofshit = a - b
		if fuckuguyspieaceofshit > 0:
			ans.append(0)
		else:
			ans.append(abs(fuckuguyspieaceofshit))
	else:
		input()
		ans.append(0)
for i in range(n):
	print(ans[i])


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	x = []
	y = []
	for i in range(n):
		a = list(map(int, input().split()))
		x.append(a[0])
		y.append(a[1])
	p = max(x) - min(y)
	if n == 1:
		print(0)
		continue
	if p < 0:
		print(0)
	else:
		print(p)


for _ in range(int(input())):
	n = int(input())
	x = 999999999999999
	y = -999999999999999
	for i in range(n):
		(a, b) = map(int, input().split())
		y = max(a, y)
		x = min(b, x)
	if n == 1:
		print(0)
	else:
		print(max(0, y - x))


for __ in range(int(input())):
	a = 0
	b = 1000000000.0 + 5
	for _ in range(int(input())):
		(x, y) = map(int, input().split(' '))
		a = max(a, x)
		b = min(b, y)
	print(max(0, a - b))


t = int(input())
for i in range(t):
	n = int(input())
	(maxl, minr) = map(int, input().split())
	for j in range(1, n):
		(l, r) = map(int, input().split())
		maxl = max(l, maxl)
		minr = min(r, minr)
	print(max(maxl - minr, 0))


t = int(input())
for i in range(t):
	n = int(input())
	k = 0
	s = 10 ** 9
	for j in range(n):
		(l, r) = map(int, input().split())
		if l > k:
			k = l
		if r < s:
			s = r
	if k - s < 0:
		print(0)
	else:
		print(k - s)


for i in range(int(input())):
	a = []
	b = []
	for q in range(int(input())):
		k = input().split()
		a.append(int(k[0]))
		b.append(int(k[1]))
	print(int((abs(max(a) - min(b)) + max(a) - min(b)) / 2))


def main():
	import os
	from io import BytesIO
	input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
	for i in range(int(input())):
		n = int(input())
		min_ = 1000000000000000000
		max_ = -min_
		for j in range(n):
			(a, b) = map(int, input().split())
			max_ = max(a, max_)
			min_ = min(b, min_)
		print(max(max_ - min_, 0))
main()


t = int(input())
for i in range(t):
	a = []
	b = []
	n = int(input())
	for j in range(n):
		(l, r) = map(int, input().split())
		a.append(l)
		b.append(r)
	a.sort()
	b.sort()
	if a[-1] <= b[0]:
		print(0)
	else:
		print(a[-1] - b[0])


for _ in range(int(input())):
	n = int(input())
	(l, r) = (0, 9999999999999)
	for i in range(n):
		(a, b) = map(int, input().split())
		l = max(l, a)
		r = min(r, b)
	print(max(0, l - r))


for t in range(int(input())):
	n = int(input())
	r_min = 10 ** 9 + 1
	l_max = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		if r < r_min:
			r_min = r
		if l > l_max:
			l_max = l
	print(max(l_max - r_min, 0))


k = int(input())
for i in range(k):
	X = []
	Y = []
	f = int(input())
	for j in range(f):
		(x, y) = map(int, input().split())
		X += [x]
		Y += [y]
	print(-min(Y) + max(X) if -min(Y) + max(X) > 0 else 0)


t = int(input())
lengths = []
for i_t in range(t):
	n = int(input())
	for i_n in range(n):
		(left, right) = input().split()
		left = int(left)
		right = int(right)
		if i_n == 0:
			left_common = left
			right_common = right
			flag = True
		elif flag:
			if left_common < left < right_common:
				left_common = left
			if left_common < right < right_common:
				right_common = right
			if right == left_common:
				flag = False
				left_common = right_common = right
			elif right < left_common:
				flag = False
				(right_common, left_common) = (left_common, right)
			if left == right_common:
				flag = False
				left_common = right_common = left
			elif right_common < left:
				flag = False
				(left_common, right_common) = (right_common, left)
		else:
			if left_common > right:
				left_common = right
			if right_common < left:
				right_common = left
	if flag:
		lengths.append(0)
	else:
		lengths.append(right_common - left_common)
for length in lengths:
	print(length)


import math
t = int(input())
for cnt in range(t):
	n = int(input())
	b = -math.inf
	a = math.inf
	for i in range(n):
		(l, r) = map(int, input().split())
		b = max(b, l)
		a = min(r, a)
	print((b - a) * int(b >= a))


def sortt(x):
	return x[1]
for i in range(int(input())):
	s = []
	n = int(input())
	for j in range(n):
		s.append(list(map(int, input().split())))
	s.sort(key=sortt)
	for j in range(len(s)):
		if j == 0:
			minn = s[j][1]
			maxx = minn
		elif s[j][0] > minn:
			if maxx < s[j][0]:
				maxx = s[j][0]
	print(maxx - minn)


t = int(input())
for q in range(0, t):
	n = int(input())
	a = []
	for i in range(0, n):
		(c, d) = map(int, input().split())
		a.append([c, 0])
		a.append([d, 1])
	a.sort()
	bal = 0
	r = 0
	l = 0
	for i in range(0, 2 * n):
		if a[i][1] == 0:
			bal += 1
		else:
			if bal != n:
				l = a[i][0]
			break
	a.reverse()
	bal = 0
	for j in range(0, 2 * n):
		if a[j][1] == 1:
			bal += 1
		else:
			if bal != n:
				r = a[j][0]
			break
	print(r - l)


t = int(input())
for i in range(t):
	n = int(input())
	minn = 1000000000.0
	maxx = -1
	for i in range(n):
		(x, y) = map(int, input().split())
		minn = min(y, minn)
		maxx = max(x, maxx)
	print(max(maxx - minn, 0))


t = int(input())
minn = 1000000001
maxx = 0
for test in range(t):
	n = int(input())
	for line in range(n):
		(x1, x2) = input().split()
		x1 = int(x1)
		x2 = int(x2)
		minn = min(x2, minn)
		maxx = max(x1, maxx)
	ans = maxx - minn
	minn = 1000000001
	maxx = 0
	if ans < 0:
		print(0)
	else:
		print(ans)


for i in range(int(input())):
	n = int(input())
	maxl = 0
	minr = 1000000000
	if n == 1:
		(l, r) = map(int, input().split())
		print(0)
	else:
		f = 0
		prevl = -1
		prevr = -1
		for i in range(n):
			(l, r) = map(int, input().split())
			if f == 0:
				if prevl == -1:
					prevl = l
					prevr = r
				elif not (l <= prevr and l >= prevl or (r >= prevl and r <= prevr) or (r >= prevr and l <= prevl)):
					f = 1
					prevl = l
					prevr = r
				elif f == 0:
					prevl = max(l, prevl)
					prevr = min(r, prevr)
			if maxl < l:
				maxl = l
			if minr > r:
				minr = r
		if f == 0:
			print(0)
		else:
			print(max(maxl, minr) - min(maxl, minr))


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		l_ans = 10 ** 9
		r_ans = -10 ** 9
		for j in range(n):
			(l, r) = map(int, input().split())
			l_ans = min(l_ans, r)
			r_ans = max(r_ans, l)
		if l_ans > r_ans:
			print(0)
		else:
			print(r_ans - l_ans)
main()


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	import math
	tc = int(input())
	for _ in range(tc):
		n = int(input())
		arr = []
		x = 0
		y = 0
		for i in range(n):
			(x, y) = input().split()
			x = int(x)
			y = int(y)
			arr.append([x, y])
		arr = sorted(arr)
		y = arr[n - 1][0]
		arr.sort(key=lambda x: x[1])
		x = arr[0][1]
		print(max(y - x, 0))
	pass
main()


import sys
import time
import itertools
import math

def inp():
	ln = input().split()
	return int(ln[0]) if len(ln) == 1 else [int(i) for i in ln]

def prl(x):
	print(' '.join([str(i) for i in x]))
testcases = inp()
for testcase in range(testcases):
	n = inp()
	a = []
	for i in range(n):
		a.append(inp())
	l = 10 ** 10
	h = 0
	for i in a:
		l = min(l, i[1])
		h = max(h, i[0])
	if h > l:
		print(h - l)
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	d = []
	for j in range(n):
		(b, c) = map(int, input().split())
		a.append(b)
		d.append(c)
	k1 = max(a)
	k2 = min(d)
	if k1 <= k2:
		print(0)
	else:
		print(k1 - k2)


t = int(input())
for i1 in range(t):
	n = int(input())
	(a, b) = (0, 10 ** 9)
	for i in range(n):
		(c, d) = list(map(int, input().split()))
		b = min(d, b)
		a = max(a, c)
	print(max(0, a - b))


n = int(input())
i = 0
for i in range(n):
	t = int(input())
	j = 0
	m = 1000000000
	M = 1
	for j in range(t):
		l = input().split()
		a = int(l[0])
		b = int(l[1])
		if a > M:
			M = a
		else:
			M = M
		if b < m:
			m = b
		else:
			m = m
		j = j + 1
	if m < M:
		print(M - m)
	else:
		print(0)
i = i + 1


for _ in range(int(input())):
	n = int(input())
	l = []
	r = []
	for i in range(n):
		(li, ri) = map(int, input().split())
		l.append(li)
		r.append(ri)
	if n == 1:
		print(0)
		continue
	r.sort()
	l.sort(reverse=True)
	if l[0] - r[0] < 0:
		print(0)
	else:
		print(l[0] - r[0])


t = int(input())
for i in range(t):
	n = int(input())
	a = [0] * n
	b = [0] * n
	for j in range(n):
		(a[j], b[j]) = map(int, input().split())
	a = max(a)
	b = min(b)
	if a - b > 0:
		print(a - b)
	else:
		print(0)


def f(lol):
	ll = [l[0] for l in lol]
	rl = [l[1] for l in lol]
	l = min(rl)
	r = max(ll)
	return 0 if l >= r else r - l
q = int(input())
for _ in range(q):
	n = int(input())
	l = [list(map(int, input().split())) for _ in range(n)]
	print(f(l))


t = int(input())
for _ in range(t):
	n = int(input())
	t = []
	for _ in range(n):
		(l, r) = map(int, input().split())
		t.append([l, r])
	t = sorted(t, key=lambda x: x[1])
	mx = t[-1][0] - t[0][1]
	for i in range(1, n - 1):
		mx = max(mx, t[i][0] - t[0][1], t[-1][0] - t[i][1])
	print(max(mx, 0))


import sys
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		(a, b) = map(int, input().split())
		print(0)
		continue
	else:
		Min = sys.maxsize
		Max = -sys.maxsize - 1
		for _ in range(n):
			(a, b) = map(int, input().split())
			Min = min(Min, b)
			Max = max(Max, a)
	print(max(0, Max - Min))


import math

def function(n, nums_0, nums_1):
	if max(nums_0) - min(nums_1) >= 0:
		return max(nums_0) - min(nums_1)
	else:
		return 0
t = int(input())
nums_0 = []
nums_1 = []
for k1 in range(t):
	n = int(input())
	for k2 in range(n):
		nums = list(map(int, input().rstrip().split()))
		nums_0.append(nums[0])
		nums_1.append(nums[1])
	print(function(n, nums_0, nums_1))
	nums_0 = []
	nums_1 = []


t = int(input())
l = []
r = []
for _ in range(t):
	n = int(input())
	for i in range(n):
		inp = input().split()
		l.append(int(inp[0]))
		r.append(int(inp[1]))
	x = max(l) - min(r)
	if x < 0:
		print(0)
	else:
		print(x)
	r.clear()
	l.clear()


t = int(input())
for i in range(t):
	n = int(input())
	max_l = 0
	min_r = 10 ** 10
	for i in range(n):
		(l, r) = map(int, input().split())
		max_l = max(max_l, l)
		min_r = min(min_r, r)
	print(max(0, max_l - min_r))


t = int(input())
for T in range(t):
	n = int(input())
	arr = [tuple(map(int, input().split())) for i in range(n)]
	arr.sort(key=lambda x: x[1])
	a = arr[0][1]
	arr.sort(key=lambda x: -x[0])
	b = arr[0][0]
	if a <= b:
		print(b - a)
	else:
		print(0)


z = int(input())
for i in range(z):
	a = int(input())
	(r, l) = map(int, input().split())
	for j in range(a - 1):
		(r1, l1) = map(int, input().split())
		r = max(r, r1)
		l = min(l, l1)
	if r < l:
		print(0)
	else:
		print(r - l)


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	mir = 10000000000000.0
	mal = 0
	for _ in range(n):
		(l, r) = map(int, input().split())
		mal = max(mal, l)
		mir = min(mir, r)
	print(max(mal - mir, 0))


a = int(input())
Ans = []
for i in range(a):
	A = []
	x = int(input())
	B = []
	for j in range(x):
		(n, m) = list(map(int, input().split()))
		A.append(n)
		B.append(m)
	A.sort()
	B.sort()
	Ans.append(max(0, A[-1] - B[0]))
for b in Ans:
	print(b)


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	for j in range(n):
		a.append([int(i) for i in input().split()])
	ans1 = a[0][1]
	ans2 = a[0][0]
	for i in a:
		ans1 = min(ans1, i[1])
		ans2 = max(ans2, i[0])
	if ans1 > ans2:
		print(0)
	else:
		print(ans2 - ans1)


t = int(input())
for i in range(t):
	n = int(input())
	min_r = 10 ** 10
	max_l = -10 ** 10
	for j in range(n):
		(a, b) = map(int, input().split())
		if a > max_l:
			max_l = a
		if b < min_r:
			min_r = b
	print(max(0, -min_r + max_l))


for i in range(int(input())):
	l = 1
	r = 10 ** 9
	for j in range(int(input())):
		(a, b) = map(int, input().split())
		(l, r) = (max(a, l), min(b, r))
	print(max(l - r, 0))


import sys, copy
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	start = []
	end = []
	for _ in range(n):
		(a, b) = map(int, sys.stdin.readline().split())
		start.append(a)
		end.append(b)
	start.sort(reverse=True)
	end.sort()
	print(start[0] - end[0] if start[0] - end[0] >= 0 else 0)


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	r = []
	for i in range(n):
		(x, y) = map(int, input().split())
		l.append(x)
		r.append(y)
	l.sort()
	r.sort()
	print(max(0, l[-1] - r[0]))


t = int(input())
otv = list()
for i in range(t):
	x = 1
	y = 1000000001
	n = int(input())
	for j in range(n):
		(a, b) = map(int, input().split())
		x = max(a, x)
		y = min(b, y)
	otv.append(max(0, x - y))
for i in otv:
	print(i)


n = int(input())
maxa = -1
minb = 1000000000
for i in range(n):
	m = int(input())
	for j in range(m):
		(a, b) = map(int, input().split())
		if a > maxa:
			maxa = a
		if b < minb:
			minb = b
	if maxa - minb < 0:
		print(0)
	else:
		print(maxa - minb)
	maxa = -1000000000
	minb = 1000000000


for _ in ' ' * int(int(input())):
	z = 0
	z1 = 10 ** 9
	for i in range(int(input())):
		(a, b) = map(int, input().split())
		z = max(a, z)
		z1 = min(b, z1)
	print(max(0, z - z1))


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(t1, t2) = map(int, input().split())
		a.append(t1)
		b.append(t2)
	if n == 1 or max(a) - min(b) < 0:
		print(0)
		continue
	print(max(a) - min(b))


for t in range(int(input())):
	mn = 1000000005
	mx = 0
	for n in range(int(input())):
		(l, r) = map(int, input().split())
		mx = max(mx, l)
		mn = min(mn, r)
	s = mx - mn
	print(max(0, s))


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	b = []
	c = []
	for j in range(n):
		b.append(list(map(int, input().split(' '))))
	for j in range(n):
		a.append(b[j][0])
		c.append(b[j][1])
	a = max(a)
	c = min(c)
	if a >= c:
		print(a - c)
	else:
		print('0')


ar_max = []
ar_min = []
answer = []
t = int(input())
for i in range(t):
	n = int(input())
	for i in range(n):
		(x, y) = map(int, input().split())
		ar_max.append(x)
		ar_min.append(y)
	if n == 1 or max(ar_max) < min(ar_min):
		answer.append(0)
	else:
		answer.append(max(ar_max) - min(ar_min))
	ar_max = []
	ar_min = []
for i in range(len(answer)):
	print(answer[i])


t = int(input())
for i in range(t):
	n = int(input())
	ml = 1
	mr = 10 ** 9
	for j in range(n):
		(l, r) = map(int, input().split())
		if l > ml:
			ml = l
		if r < mr:
			mr = r
	if ml > mr:
		print(ml - mr)
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	l = 0
	r = 10 ** 9 + 1
	for j in range(n):
		(l_i, r_i) = map(int, input().split())
		if r_i < r:
			r = r_i
		if l_i > l:
			l = l_i
	ans = max(0, l - r)
	print(ans)


t = int(input())
for test in range(t):
	i = int(input())
	lines = []
	minl = None
	minr = None
	for line in range(i):
		(l, r) = list(map(int, input().split()))
		if minl == None:
			minl = r
			minr = r
		else:
			minr = min(minr, r)
		lines.append([l, r])
	for line in lines:
		if minl == None or line[1] < minl:
			minl = line[1]
		if minr == None or line[0] > minr:
			minr = line[0]
	print(minr - minl)


t = int(input())
left = []
right = []
for i in range(t):
	n = int(input())
	for j in range(n):
		(l, r) = map(int, input().split())
		left.append(l)
		right.append(r)
	if max(left) - min(right) > 0:
		print(max(left) - min(right))
	else:
		print(0)
	left.clear()
	right.clear()


t = int(input())
for w in range(t):
	n = int(input())
	if n == 1:
		(l, r) = (int(i) for i in input().split())
		print(0)
	else:
		(l1, r1) = (int(i) for i in input().split())
		for i in range(n - 1):
			(l, r) = (int(i) for i in input().split())
			l1 = max(l, l1)
			r1 = min(r, r1)
		print(max(0, l1 - r1))


n = int(input())
for k in range(0, n):
	l = int(input())
	o = 10 ** 10
	q = 0
	for k in range(0, l):
		(m, r) = map(int, input().split())
		if r < o:
			o = r
		if m > q:
			q = m
	if l == 1:
		print(0)
	elif q - o <= 0:
		print(0)
	else:
		print(q - o)


for i in range(int(input())):
	a = -1
	q = 10 ** 9
	for j in range(int(input())):
		u = [int(i) for i in input().split()]
		if a < u[0]:
			a = u[0]
		if q > u[1]:
			q = u[1]
	print(max(a - q, 0))


lArr = []
rArr = []
lMax = 0
rMin = 0
t = int(input())
for i in range(t):
	n = int(input())
	for j in range(n):
		(l, r) = [int(x) for x in input().split()]
		lArr.append(l)
		rArr.append(r)
	lMax = max(lArr)
	rMin = min(rArr)
	if lMax - rMin >= 0:
		print(lMax - rMin)
	else:
		print(0)
	lArr = []
	rArr = []


for i in range(int(input())):
	r = list()
	l = list()
	for i in range(int(input())):
		s = input().split()
		l.append(int(s[0]))
		r.append(int(s[1]))
	a = ((min(r) - max(l)) ** 2) ** 0.5
	if min(r) >= max(l):
		a = 0
	print(int(a))


for i in range(int(input())):
	minn = 10 ** 9 + 1
	maxx = 1
	n = int(input())
	for j in range(n):
		(a, b) = map(int, input().split())
		if a > maxx:
			maxx = a
		if b < minn:
			minn = b
	if minn > maxx:
		print(0)
	else:
		print(abs(maxx - minn))


t = int(input())
while t:
	t += -1
	n = int(input())
	al = 0
	ar = 9999999999
	c = 0
	for i in range(n):
		(l, r) = [int(x) for x in input().split()]
		if c == 0:
			al = max(al, l)
			ar = min(ar, r)
		if c == 0 and al > ar:
			(al, ar) = (ar, al)
			c = 1
			continue
		if c == 1:
			if al <= l <= ar or al <= r <= ar:
				continue
			elif r < al:
				al = r
			elif l > ar:
				ar = l
	if c == 0:
		print(0)
	else:
		print(ar - al)


for _ in range(int(input())):
	n = int(input())
	L = []
	R = []
	for i in range(n):
		(l, r) = map(int, input().split())
		L.append(l)
		R.append(r)
	if n == 1:
		print(0)
	else:
		temp = max(L) - min(R)
		print(max(0, temp))


from sys import stdin
t = int(stdin.readline())
for q in range(t):
	n = int(stdin.readline())
	(m1, m2) = (10 ** 9 + 1, 0)
	for i in range(n):
		(l, r) = map(int, stdin.readline().split())
		m1 = min(m1, r)
		m2 = max(m2, l)
	print(max(0, m2 - m1))


kT = int(input())
ans = []
for iter in range(kT):
	n = int(input())
	beg = float('inf')
	end = -float('inf')
	for i in range(n):
		(l, r) = map(int, input().split())
		beg = min(r, beg)
		end = max(l, end)
	xxx = end - beg
	if xxx <= 0:
		ans.append(0)
	else:
		ans.append(xxx)
print(*ans, sep='\n')


n = int(input())
oo = 10 ** 9 + 7
for i in range(n):
	m = int(input())
	Min_kon = oo
	Max_nach = 0
	for j in range(m):
		(a, b) = map(int, input().split())
		if b < Min_kon:
			Min_kon = b
		if a > Max_nach:
			Max_nach = a
	if Min_kon <= Max_nach:
		print(Max_nach - Min_kon)
	else:
		print(0)


t_sets = int(input())
low = 'a'
high = 'b'
for i in range(0, t_sets):
	n_cuts = int(input())
	list_of_cuts = []
	for i in range(0, n_cuts):
		cut1 = input()
		cut = [int(x) for x in cut1.split(' ')]
		list_of_cuts.append(cut)
	if len(list_of_cuts) > 1:
		for cut in list_of_cuts:
			if low == 'a':
				low = cut[1]
			if cut[1] < low:
				low = cut[1]
			if high == 'b':
				high = cut[0]
			if cut[0] > high:
				high = cut[0]
		if high > low:
			print(high - low)
		else:
			print(0)
	else:
		print(0)
	low = 'a'
	high = 'b'


for _ in range(int(input())):
	l = []
	r = []
	for _ in range(int(input())):
		(j, k) = [int(x) for x in input().split()]
		l += [j]
		r += [k]
	g = max(l) - min(r)
	if g <= 0:
		print(0)
	else:
		print(g)


t = int(input())
for _ in range(t):
	n = int(input())
	ans = [1000000000.0, 1]
	for _ in range(n):
		(l, r) = map(int, input().split())
		ans[0] = min(ans[0], r)
		ans[1] = max(ans[1], l)
	if ans[1] - ans[0] > 0:
		print(ans[1] - ans[0])
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	(x, y, f) = (*[int(x) for x in input().split()], True)
	for _ in range(n - 1):
		(a, b) = [int(x) for x in input().split()]
		if f:
			if a > y:
				(x, y, f) = (y, a, False)
			elif b < x:
				(x, y, f) = (b, x, False)
			else:
				(x, y) = (max(x, a), min(y, b))
		elif a > y:
			y = a
		elif b < x:
			x = b
	print(0 if f else y - x)


import sys
input = sys.stdin.readline

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())
t = inp()
for i in range(t):
	n = inp()
	highl = -1
	lowr = 1000000000
	for i in range(n):
		x = inlt()
		highl = max(x[0], highl)
		lowr = min(x[1], lowr)
	if n == 1 or highl - lowr < 0:
		print(0)
	else:
		print(highl - lowr)


t = int(input())
for i in range(t):
	n = int(input())
	left = -10000000000
	right = 10000000000
	for j in range(n):
		(a, b) = map(int, input().split())
		if a > left:
			left = a
		if b < right:
			right = b
	if right >= left:
		print(0)
	else:
		print(abs(right - left))


t = int(input())
for q in range(t):
	n = int(input())
	max_l = -1
	min_r = 10 ** 9 + 7
	for i in range(n):
		(l, r) = map(int, input().split())
		max_l = max(max_l, l)
		min_r = min(min_r, r)
	if n == 1 or min_r > max_l:
		print(0)
	else:
		print(max_l - min_r)


t = int(input())
i = 0
while i < t:
	n = int(input())
	j = 0
	X = 0
	Y = 1000000001
	while j < n:
		koord = input().split()
		x = int(koord[0])
		y = int(koord[1])
		if y < Y:
			Y = y
		if x > X:
			X = x
		j += 1
	answer = X - Y
	if answer <= 0:
		print(0)
	else:
		print(answer)
	i += 1


t = int(input())
for _ in range(t):
	n = int(input())
	(first, last) = (10 ** 10, 0)
	for i in range(n):
		(l, r) = [int(x) for x in input().split()]
		if r < first:
			first = r
		if l > last:
			last = l
	res = last - first
	if res >= 0:
		print(res)
	else:
		print(0)


tests = int(input())
for _ in range(tests):
	n = int(input())
	maxl = -float('inf')
	minr = float('inf')
	for i in range(n):
		(a, b) = map(int, input().split())
		maxl = max(maxl, a)
		minr = min(minr, b)
	print(max(0, maxl - minr))


t = int(input())
for i in range(t):
	n = int(input())
	points = []
	sorted_list = []
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		points.append((a, b))
	sort_second = sorted(points, key=lambda x: x[1])
	sort_first = sorted(points, key=lambda x: x[0])
	ans = sort_first[-1][0] - sort_second[0][1]
	if ans < 0:
		print(0)
	else:
		print(ans)


t = int(input())
for _i in range(t):
	n = int(input())
	f = True
	(ml, mr) = (-1, pow(10, 9) + 1)
	for i in range(n):
		(l, r) = map(int, input().split())
		if max(ml, mr) < l or min(ml, mr) > r:
			f = False
		ml = max(ml, l)
		mr = min(mr, r)
	if f:
		print(0)
	else:
		print(abs(ml - mr))


from math import *
import os, sys
from io import BytesIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
for i in range(int(input())):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(x, y) = map(int, input().split())
		a.append((x, y))
		b.append((y, x))
	a.sort()
	b.sort()
	if n == 1:
		print(0)
	else:
		l = b[0][0]
		r = a[-1][0]
		print(max(r - l, 0))


inf = 10000000000.0
for _ in range(int(input())):
	n = int(input())
	(maxa, minb) = (-inf, inf)
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		maxa = max(a, maxa)
		minb = min(b, minb)
	print(max(maxa - minb, 0))


t = int(input())
for z in range(t):
	n = int(input())
	arr = []
	lr = 1000000000000
	rl = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		lr = min(lr, r)
		rl = max(rl, l)
		arr.append([l, r])
	ans = rl - lr
	if ans < 0:
		print(0)
	else:
		print(ans)


n = int(input())
for i in range(n):
	k = int(input())
	ma = 0
	mb = 10000000000
	for j in range(k):
		(a, b) = [int(i) for i in input().split()]
		if a > ma:
			ma = a
		if b < mb:
			mb = b
	print(ma - mb if ma - mb > -1 else 0)


t = int(input())
for request in range(t):
	n = int(input())
	(a, b) = ([], [])
	mxA = 0
	for i in range(n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
		if x > mxA:
			mxA = x
	(mn, mnB) = (10 ** 10, 10 ** 10)
	for i in range(n):
		if b[i] >= mxA and b[i] < mnB:
			mnB = b[i]
		if b[i] < mn:
			mn = b[i]
	print(min(abs(mnB - mn), abs(mxA - mn)))


a = int(input())
for i in range(a):
	maxl = 0
	minR = 1000000001
	b = int(input())
	if b == 1:
		(l, r) = map(int, input().split())
		print(0)
	else:
		for i in range(b):
			(l, r) = map(int, input().split())
			if l > maxl:
				maxl = l
			if r < minR:
				minR = r
		if maxl < minR:
			print(0)
		else:
			print(maxl - minR)


from sys import stdin
import math
t = int(stdin.readline())
while t:
	n = int(stdin.readline())
	s = []
	for _ in range(n):
		(l, r) = map(int, stdin.readline().split())
		s.append([l, r])
	s.sort(key=lambda x: x[1])
	m = 0
	(a, b) = (s[0][0], s[0][1])
	for e in s[1:]:
		if e[0] > b:
			m += e[0] - b
			b = e[0]
	print(m)
	t -= 1


n = int(input())
for i in range(n):
	m = int(input())
	r = 999999999999999999
	l = 0
	typ = 0
	for i in range(m):
		(a, b) = map(int, input().split())
		if typ == 0:
			if a < l and b < l:
				r = l
				l = b
				typ = 1
			elif b > r and a > r:
				l = r
				r = a
				typ = 1
			else:
				minr = min([r, b])
				maxl = max([l, a])
				l = maxl
				r = minr
				typ = 0
		elif a < l and b < l:
			l = b
		elif b > r and a > r:
			r = a
	if typ == 0:
		print(0)
	else:
		print(r - l)


import math
tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = []
	x = 0
	y = 0
	for i in range(n):
		(x, y) = input().split()
		x = int(x)
		y = int(y)
		arr.append([x, y])
	arr = sorted(arr)
	y = arr[n - 1][0]
	arr.sort(key=lambda x: x[1])
	x = arr[0][1]
	print(max(y - x, 0))


t = int(input())
a = []
for i in range(t):
	n = int(input())
	arr = []
	for j in range(n):
		str = input().split()
		arr.append([int(str[0]), int(str[1])])
	a.append(arr)
for i in range(len(a)):
	arr = a[i]
	max_e = 0
	min_e = 1000000000
	for j in range(len(arr)):
		if arr[j][0] > max_e:
			max_e = arr[j][0]
		if arr[j][1] < min_e:
			min_e = arr[j][1]
	if min_e >= max_e:
		print(0)
	else:
		print(max_e - min_e)


INF = 10 ** 9
tests = int(input())
for test in range(tests):
	n = int(input())
	min_r = INF
	max_l = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		max_l = max(l, max_l)
		min_r = min(r, min_r)
	if max_l <= min_r:
		print(0)
	else:
		print(max_l - min_r)


for _ in range(int(input())):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(ai, bi) = list(map(int, input().split()))
		a.append(ai)
		b.append(bi)
	if n == 1:
		print(0)
	elif max(a) < min(b):
		print(0)
	else:
		print(max(a) - min(b))


t = int(input())
for j in range(t):
	n = int(input())
	minn = 999999999999999
	maxn = 0
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		if minn > b:
			minn = b
		if maxn < a:
			maxn = a
	if maxn - minn < 0:
		print(0)
	else:
		print(maxn - minn)


t = int(input())
for qw in range(t):
	n = int(input())
	a1 = 0
	b1 = 100000000000
	for i in range(n):
		(a, b) = map(int, input().split())
		if a > a1:
			a1 = a
		if b < b1:
			b1 = b
	print(max(0, a1 - b1))


for _ in range(int(input())):
	n = int(input())
	data = []
	max_l = -1
	min_r = 10 ** 10
	for i in range(n):
		(l, r) = map(int, input().split())
		max_l = max(max_l, l)
		min_r = min(min_r, r)
	print(max(max_l - min_r, 0))


t = int(input())
answer = []
for i in range(t):
	n = int(input())
	(ns, nf) = [int(j) for j in input().split()]
	for k in range(n - 1):
		(a, b) = [int(j) for j in input().split()]
		if a > ns:
			ns = a
		if b < nf:
			nf = b
	if ns > nf:
		answer.append(ns - nf)
	else:
		answer.append(0)
for i in answer:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	(l, r) = (0, 10000000000.0)
	for j in range(n):
		(lt, rt) = map(int, input().split())
		l = max(l, lt)
		r = min(r, rt)
	print(max(0, l - r))


for _ in range(int(input())):
	(x, y) = (1, 10 ** 9)
	for _ in range(int(input())):
		(L, R) = map(int, input().split())
		(x, y) = (max(x, L), min(y, R))
	print(max(0, x - y))


for i in range(int(input())):
	n = int(input())
	e = 10000000000000000
	s = -10000000000000000
	for i in range(n):
		(a, b) = map(int, input().split())
		e = min(e, b)
		s = max(a, s)
	print(max(0, s - e))


t = int(input())
for _ in range(t):
	a = []
	n = int(input())
	for _ in range(n):
		(m1, m2) = map(int, input().split())
		a.append((m1, m2))
	x = min(a, key=lambda x: x[1])[1]
	y = max(a, key=lambda x: x[0])[0]
	if y - x <= 0:
		print(0)
	else:
		print(y - x)


class Coord:

	def __init__(self, a, b):
		self.a = a
		self.b = b
ans = []
am1 = int(input())
for i in range(am1):
	c = 0
	am2 = int(input())
	ar = []
	for j in range(am2):
		stop = False
		(a, b) = map(int, input().split())
		nc = Coord(a, b)
		k = 0
		while k < c and (not stop):
			if not (ar[k].a > nc.b or ar[k].b < nc.a):
				ar[k].a = max(ar[k].a, nc.a)
				ar[k].b = min(ar[k].b, nc.b)
				stop = True
			k += 1
		if not stop:
			ar.append(nc)
			c += 1
	if c != 1:
		mi = ar[0].b
		ma = ar[0].a
		for j in range(c):
			if ar[j].b <= mi:
				mi = ar[j].b
			if ar[j].a >= ma:
				ma = ar[j].a
		ans.append(abs(ma - mi))
	else:
		ans.append(0)
for i in range(am1):
	print(ans[i])


n = int(input())
for i in range(n):
	t = int(input())
	for i in range(t):
		(a, b) = map(int, input().split())
		if i == 0:
			biggest_a = a
			smallest_b = b
		if biggest_a < a:
			biggest_a = a
		if smallest_b > b:
			smallest_b = b
			pass
	if biggest_a < smallest_b:
		biggest_a = smallest_b
	if t == 1:
		print(0)
	else:
		print(abs(biggest_a - smallest_b))


otvet = []
t = int(input())
for i in range(t):
	n = int(input())
	for j in range(n):
		if j == 0:
			(min2, max1) = reversed(list(map(int, input().split())))
		else:
			(a, b) = map(int, input().split())
			if a > max1:
				max1 = a
			if b < min2:
				min2 = b
	z = max1 - min2
	if z > 0:
		otvet.append(z)
	else:
		otvet.append(0)
for i in otvet:
	print(i)


n = int(input())
for i in range(n):
	k = int(input())
	l = 1000000000000
	r = 0
	for i in range(k):
		(a, b) = list(map(int, input().split()))
		if a > r:
			r = a
		if l > b:
			l = b
	if l >= r:
		print(0)
	else:
		print(r - l)


t = int(input())
for tt in range(t):
	n = int(input())
	mn = 1000000002
	mx = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		mn = min(mn, r)
		mx = max(mx, l)
	print(max(mx - mn, 0))


n = int(input())
for i in range(n):
	l = int(input())
	cur = [int(k) for k in input().split()]
	Min = cur[1]
	Max = cur[0]
	for i2 in range(l - 1):
		cur = [int(k) for k in input().split()]
		Min = min(cur[1], Min)
		Max = max(cur[0], Max)
	print(max(Min, Max) - Min)


t = int(input())
for i in range(t):
	n = int(input())
	mi = 10000000000
	ma = 0
	for k in range(n):
		l = input().split(' ')
		if int(l[0]) > ma:
			ma = int(l[0])
		if int(l[1]) < mi:
			mi = int(l[1])
	if mi >= ma:
		print(0)
	else:
		print(ma - mi)


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	l = 10 ** 9
	r = 0
	for i in range(n):
		(x, y) = map(int, input().split())
		if y < l:
			l = y
		if x > r:
			r = x
	print(max(0, r - l))


ans = []
n = int(input())
for _ in range(n):
	k = int(input())
	(m_b, m_a) = map(int, input().split())
	for _ in range(k - 1):
		(a, b) = map(int, input().split())
		m_a = min(m_a, b)
		m_b = max(m_b, a)
	if m_a > m_b:
		ans.append(0)
	else:
		ans.append(m_b - m_a)
for el in ans:
	print(el)


t = int(input())
for i in range(t):
	n = int(input())
	mi = 10 ** 100
	ma = 0
	for j in range(n):
		(a, b) = [int(x) for x in input().split()]
		mi = min(mi, b)
		ma = max(a, ma)
	print(max(ma - mi, 0))


for _ in range(int(input())):
	n = int(input())
	ans = list(map(int, input().split()))
	for i in range(n - 1):
		line = list(map(int, input().split()))
		if line[0] > ans[0]:
			ans[0] = line[0]
		if line[1] < ans[1]:
			ans[1] = line[1]
	print(ans[0] - ans[1] if ans[0] > ans[1] else 0)


t = int(input())
for sadf in range(t):
	n = int(input())
	max_left = 0
	min_right = 1000000000001
	for i in range(n):
		(a, b) = map(int, input().split())
		max_left = max(max_left, a)
		min_right = min(min_right, b)
	print(max(0, max_left - min_right))


t = int(input())
for q in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		(x, y) = map(int, input().split())
		arr.append((x, y))
	arr.sort(key=lambda x: x[1])
	l = arr[0][-1]
	arr.sort(key=lambda x: x[0])
	r = arr[-1][0]
	res = r - l
	if res < 0:
		res = 0
	print(res)


for i in range(int(input())):
	l = -1
	r = 10 ** 9
	for j in range(int(input())):
		u = [int(i) for i in input().split()]
		if l < u[0]:
			l = u[0]
		if r > u[1]:
			r = u[1]
	print(max(l - r, 0))


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	n = get_int()
	(mx, mi) = get_list()
	for i in range(n - 1):
		(x, y) = get_list()
		mx = max(mx, x)
		mi = min(mi, y)
	print(max(mx - mi, 0))


t = int(input())
for i in range(t):
	a = []
	b = []
	n = int(input())
	for j in range(n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	l = max(a)
	r = min(b)
	out = max(0, l - r)
	print(out)


t = int(input())
seq = dict()
ans = []
for i in range(t):
	n = int(input())
	seq[i] = []
	for j in range(n):
		line = list(map(int, input().split(' ')))
		seq[i].append(line)
for key in seq.keys():
	max_a = 0
	min_b = float('inf')
	for val in seq[key]:
		(a, b) = val
		if a > max_a:
			max_a = a
		if b < min_b:
			min_b = b
	ans.append(max_a - min_b if max_a - min_b > -1 else 0)
for i in ans:
	print(i)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	l1 = [[int(i) for i in input().split()] for j in range(n)]
	lmax = -1
	rmin = 10 ** 9 + 7
	for i in range(n):
		lmax = max(lmax, l1[i][0])
		rmin = min(rmin, l1[i][1])
	print(max(0, lmax - rmin))


for _ in range(int(input())):
	(L, R) = (0, 10 ** 9 + 1)
	for _ in range(int(input())):
		(l, r) = map(int, input().split())
		L = max(L, l)
		R = min(R, r)
	print(max(0, L - R))


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	seg = []
	for j in range(n):
		ls = list(map(int, input().split()))
		seg.append(ls)
	left = seg[0][1]
	right = seg[0][0]
	for k in range(n):
		if seg[k][1] < left:
			left = seg[k][1]
		if seg[k][0] > right:
			right = seg[k][0]
	if right - left <= 0:
		ans.append(0)
	else:
		ans.append(right - left)
for h in range(t):
	print(ans[h])


import math
a = int(input())
c = []
for i in range(a):
	b = int(input())
	for j in range(b):
		c.append([])
		d = input().split()
		c[i].append([int(d[0]), int(d[1])])
for i in range(a):
	minx = []
	maxx = []
	for j in range(len(c[i])):
		minx.append(c[i][j][0])
		maxx.append(c[i][j][1])
	print(max(max(minx) - min(maxx), 0))


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
from collections import *
ORDA = 97

def ii():
	return int(input())

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

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True

def main():
	for _ in range(ii()):
		n = ii()
		arr = [0] * n
		for i in range(n):
			arr[i] = li()
		l = 1000000001
		r = -1
		for ele in arr:
			l = min(ele[1], l)
			r = max(ele[0], r)
		if n == 1:
			print(0)
		else:
			ans = r - l
			if ans < 0:
				print(0)
			else:
				print(ans)
main()


t = int(input())
for i in range(t):
	n = int(input())
	lstl = []
	lstr = []
	for i in range(n):
		(l, r) = map(int, input().split())
		lstl.append(l)
		lstr.append(r)
	print(max(max(lstl) - min(lstr), 0))


def f():
	n = int(input())
	arr = []
	l = -1
	r = -1
	for i in range(n):
		(a, b) = map(int, input().split())
		arr += [(a, 0), (b, 1)]
	arr.sort()
	for i in arr:
		if i[1] == 1:
			l = i[0]
			break
	for i in range(len(arr)):
		x = arr[-i - 1]
		if x[1] == 0:
			r = x[0]
			break
	res = max(0, r - l)
	return res
t = int(input())
for _ in range(t):
	print(f())


t = int(input())
while t > 0:
	n = int(input())
	left_max_end = 0
	right_min_end = 10 ** 9
	while n > 0:
		(a, b) = [int(i) for i in input().split()]
		if a > left_max_end:
			left_max_end = a
		if b < right_min_end:
			right_min_end = b
		n -= 1
	if left_max_end - right_min_end > 0:
		print(left_max_end - right_min_end)
	else:
		print(0)
	t -= 1


t = int(input())
for i in range(t):
	ansx = -10000000000
	ansy = 1000000000
	n = int(input())
	for i in range(n):
		(x, y) = map(int, input().split())
		ansx = max(ansx, x)
		ansy = min(y, ansy)
	print(max(ansx - ansy, 0))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(l, r) = ([], [])
	for i in range(int(input())):
		(x, y) = map(int, input().split())
		l += (x,)
		r += (y,)
	print(max(0, max(l) - min(r)))


t = int(input())
for j in range(t):
	n = int(input())
	my = 1000000001
	mx = 0
	for i in range(n):
		s = list(map(int, input().strip().split()))
		x = s[0]
		y = s[1]
		if x > mx:
			mx = x
		if y < my:
			my = y
	print(max(0, mx - my))


for t in range(int(input())):
	n = int(input())
	if n == 1:
		(a, b) = map(int, input().split())
		print(0)
	else:
		alist = []
		blist = []
		for i in range(n):
			(a, b) = map(int, input().split())
			alist.append(a)
			blist.append(b)
		print(abs(min(0, min(blist) - max(alist))))


a = int(input())
for i in range(a):
	b = int(input())
	mi = 10000000
	ma = 0
	x1 = []
	y1 = []
	for i in range(b):
		(x, y) = map(int, input().split())
		x1.append(x)
		y1.append(y)
	x = min(y1)
	y = max(x1)
	if x >= y:
		print(0)
	else:
		print(y - x)


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(n):
		a.append([int(s) for s in input().split()])
	l = []
	r = []
	for j in range(n):
		l.append(a[j][0])
		r.append(a[j][1])
	if n == 1:
		f = 0
	elif min(r) - max(l) > 0:
		f = 0
	else:
		f = abs(min(r) - max(l))
	print(f)


a = int(input())
for i in range(a):
	n = int(input())
	A = []
	B = []
	for i1 in range(n):
		(c, d) = [int(s) for s in input().split()]
		A.append(c)
		B.append(d)
	if n >= 2 and min(B) < max(A):
		k = abs(max(A) - min(B))
	else:
		k = 0
	print(k)


t = int(input())
otv = []
for i in range(t):
	n = int(input())
	mxX = 10 ** 10
	mxY = 0
	for j in range(n):
		(x, y) = map(int, input().split())
		if x > mxY:
			mxY = x
		if y < mxX:
			mxX = y
	otv.append(mxY - mxX)
for i in range(t):
	if otv[i] <= 0:
		print(0)
	else:
		print(otv[i])


I = input
pr = print
for _ in range(int(I())):
	mn = 1000000000.0
	mx = 0
	y = int(I())
	for x in range(y):
		(m, n) = map(int, I().split())
		mx = max(mx, m)
		mn = min(mn, n)
	pr(mx - mn if y > 1 and mx > mn else 0)


import sys
import bisect

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

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

def listinput():
	return list(map(int, input().split()))
for _ in range(iinput()):
	n = iinput()
	(l, r) = minput()
	for _ in range(n - 1):
		(x, y) = minput()
		r = min(r, y)
		l = max(l, x)
	print(max(0, l - r))


t = int(input())
for i in range(t):
	n = int(input())
	maxl = 0
	minr = 1000000000
	for j in range(n):
		(l, r) = map(int, input().split())
		if l > maxl:
			maxl = l
		if r < minr:
			minr = r
	if maxl - minr < 0:
		print(0)
	else:
		print(maxl - minr)


for _ in range(int(input())):
	(l, r) = (1 << 40, -(1 << 40))
	for _ in range(int(input())):
		(x, y) = map(int, input().split())
		l = min(l, y)
		r = max(r, x)
	print(max(0, r - l))


t = int(input())

def s2i(arr):
	x = input()
	x = x.split()
	for i1 in range(2):
		arr.append(int(x[i1]))
for i in range(t):
	n = int(input())
	otr1 = []
	otr2 = []
	for j in range(n):
		temp = []
		s2i(temp)
		for g in range(len(temp)):
			if g == 0:
				otr1.append(temp[g])
			else:
				otr2.append(temp[g])
	frst = min(otr2)
	sec = max(otr1)
	ans = sec - frst
	if ans > 0:
		print(ans)
	else:
		print(0)


t = int(input())
for faw in range(t):
	n = int(input())
	mal = -1
	mir = 10000000000000
	for i in range(n):
		(l, r) = map(int, input().split())
		mal = max(mal, l)
		mir = min(mir, r)
	print(max(mal - mir, 0))


n = int(input())
for i in range(n):
	n1 = int(input())
	if n1 == 1:
		print(0)
		n1 = input()
	else:
		data = []
		for j in range(n1):
			data.append(input().split())
		mi = 100000000000
		mx = -1
		for x in data:
			if int(x[1]) < mi:
				mi = int(x[1])
			if int(x[0]) > mx:
				mx = int(x[0])
		if mx - mi >= 0:
			print(mx - mi)
		else:
			print(0)


for t in range(int(input())):
	n = int(input())
	a = []
	value = None
	for i in range(n):
		z = list(map(int, input().split()))
		a.append(z)
	(low, high) = (a[0][0], a[0][1])
	for i in a[1:]:
		if i[0] > low:
			low = i[0]
		if i[1] < high:
			high = i[1]
	print(max(0, low - high))


q = int(input())
for i in range(q):
	n = int(input())
	if n == 1:
		n = input()
		print(0)
		continue
	arr = []
	for j in range(n):
		(start, end) = map(int, input().split())
		arr.append((start, end))
	arr.sort(key=lambda x: x[1])
	flag = 0
	maxims = 0
	for it in arr:
		if it[0] > arr[0][1]:
			flag = 1
			if it[0] > maxims:
				maxims = it[0]
	if flag:
		print(maxims - arr[0][1])
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	l = [-1] * 10 ** 5
	r = [10 ** 10] * 10 ** 5
	for j in range(n):
		(l[j], r[j]) = map(int, input().split())
	print(max(0, max(l) - min(r)))


test_count = int(input())
for _ in range(test_count):
	left = []
	right = []
	for __ in range(int(input())):
		current_segment = list(map(int, input().split(' ')))
		left.append(current_segment[0])
		right.append(current_segment[1])
	left.sort()
	right.sort()
	print(left[-1] - right[0] if left[-1] - right[0] > 0 else 0)


for _ in range(int(input())):
	length = []
	for _ in range(int(input())):
		length.append(list(map(int, input().split())))
	if len(length) == 1:
		print(0)
		continue
	length = sorted(length)
	left_bound = length[0][1]
	right_bound = length[-1][0]
	for i in range(len(length)):
		if length[i][1] <= left_bound:
			left_bound = length[i][1]
	print(max(0, right_bound - left_bound))


t = int(input())
while t != 0:
	t -= 1
	L = []
	K = []
	n = int(input())
	k = n
	while n != 0:
		n -= 1
		(a, b) = map(int, input().split())
		L.append(a)
		K.append(b)
	if k == 1:
		print(0)
		k -= 1
	if k > 0:
		L = sorted(L)
		K = sorted(K)
		if L[-1] - K[0] <= 0:
			print(0)
		else:
			print(L[-1] - K[0])


for _ in range(int(input())):
	n = int(input())
	(l, r) = map(int, input().split())
	for i in range(n - 1):
		(l1, r1) = map(int, input().split())
		if l1 > l:
			l = l1
		if r1 < r:
			r = r1
	if l > r:
		print(l - r)
	else:
		print(0)


t = int(input())
for _ in range(t):
	n = int(input())
	(l_a, r_a) = map(int, input().split())
	for i in range(n - 1):
		(l1, r1) = map(int, input().split())
		l_a = max(l_a, l1)
		r_a = min(r_a, r1)
	print(max(0, l_a - r_a))


t = int(input())
mas = []
for i in range(t):
	n = int(input())
	mas_x = []
	mas_y = []
	for j in range(n):
		(x, y) = map(int, input().split())
		mas_x.append(x)
		mas_y.append(y)
	mas_x.sort()
	mas_y.sort()
	x = mas_x[-1]
	y = mas_y[0]
	a = x - y
	if a < 0:
		a = 0
	mas.append(a)
for i in range(t):
	print(mas[i])


def STR():
	return list(input())

def INT():
	return int(input())

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

def MAP2():
	return map(float, input().split())

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

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
for tt in range(INT()):
	n = INT()
	arr = []
	for i in range(n):
		(l, r) = MAP()
		arr.append([l, r])
	if n == 1:
		print(0)
	else:
		arr.sort(key=lambda x: x[1])
		s_end = arr[0][1]
		arr.sort(key=lambda x: x[0], reverse=True)
		b_start = arr[0][0]
		ans = b_start - s_end
		if ans < 0:
			print(0)
		else:
			print(ans)


kolvo = int(input())
for i in range(kolvo):
	kolvo2 = int(input())
	sp1 = []
	sp2 = []
	for j in range(kolvo2):
		st = input().split()
		sp1.append(int(st[0]))
		sp2.append(int(st[1]))
	vivod = int(max(sp1)) - int(min(sp2))
	if len(sp1) == 1 or vivod <= 0:
		vivod = 0
	print(vivod)


minn = []
maxx = []
a = int(input())
for i in range(a):
	b = int(input())
	for j in range(b):
		c = input().split()
		minn.append(int(c[0]))
		maxx.append(int(c[1]))
	if max(minn) - min(maxx) > 0:
		print(max(minn) - min(maxx))
	else:
		print(0)
	minn = []
	maxx = []


m = int(input())
for i in range(m):
	n = int(input())
	c = 10000000000
	d = 0
	for j in range(n):
		(a, b) = map(int, input().split())
		if b < c:
			c = b
		if a > d:
			d = a
	if c > d:
		print(0)
	else:
		print(d - c)


from sys import stdin, stdout
from math import *
import itertools
for z in range(int(input())):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	print(max(0, max(a) - min(b)))


for i in range(int(input())):
	n = int(input())
	mn = 999999999999999999999
	mx = 0
	mx1 = 999999999999999999999999
	a = []
	for j in range(n):
		(x, y) = map(int, input().split())
		mn = min(mn, y)
		mx = max(mx, x)
	if mx < mn:
		mx = mn
	print(mx - mn)


t = int(input())
for e in range(t):
	n = int(input())
	l = [0] * n
	r = [0] * n
	for i in range(n):
		a = list(map(int, input().split()))
		l[i] = a[0]
		r[i] = a[1]
	lans = 0
	rans = 0
	for i in range(n):
		if l[i] < l[lans]:
			lans = i
		if r[i] > r[rans]:
			rans = i
	for i in range(n):
		if r[i] < r[lans]:
			lans = i
		if l[i] > l[rans]:
			rans = i
	if l[rans] < r[lans]:
		print(0)
	else:
		print(l[rans] - r[lans])


def iinput():
	return [int(i) for i in input().split()]
t = int(input())
for _ in range(t):
	n = int(input())
	max_l = 0
	min_r = 10 ** 9 + 1
	for _ in range(n):
		(l, r) = iinput()
		min_r = min(min_r, r)
		max_l = max(max_l, l)
	print(max(0, max_l - min_r))


import sys

def inp():
	return sys.stdin.readline().strip()
t = int(inp())
for i in range(t):
	n = int(inp())
	mi = 9999999999
	ma = 0
	for j in range(n):
		(a, b) = [int(i) for i in inp().split()]
		mi = min(mi, b)
		ma = max(ma, a)
	ans = ma - mi
	if ma - mi < 0:
		print(0)
	else:
		print(ans)


maximum = 0
rez = ''
minimum = 1000000000000
gg = int(input())
for i in range(1, gg + 1):
	n = int(input())
	if n > 1:
		for j in range(1, n + 1):
			(l, p) = map(int, input().split())
			if l > maximum:
				maximum = l
			if p < minimum:
				minimum = p
		if maximum - minimum > 0:
			rez = rez + '\n' + str(maximum - minimum)
		else:
			rez = rez + '\n' + '0'
	else:
		(l, p) = map(int, input().split())
		rez = rez + '\n' + '0'
	maximum = 0
	minimum = 1000000000000
print(rez)


for _ in range(int(input())):
	n = int(input())
	(me, ms) = (10 ** 18, 0)
	for i in range(n):
		(s, e) = map(int, input().split())
		if s > ms:
			ms = s
		if e < me:
			me = e
	print(max(ms - me, 0))


l = []
r = []
t = int(input())
for i in range(t):
	n = int(input())
	for j in range(n):
		s = list(map(int, input().split()))
		l += [s[0]]
		r += [s[1]]
	if min(r) < max(l):
		print(max(l) - min(r))
	else:
		print(0)
	l = []
	r = []


def ft():
	n = int(input())
	amax = None
	bmax = None
	amin = None
	bmin = None
	answer = 0
	for i in range(n):
		(a, b) = [int(s) for s in input().split()]
		if amax is None or a > amax:
			amax = a
		if bmin is None or b < bmin:
			bmin = b
	if (amax is not None and bmin is not None) and amax > bmin:
		answer = amax - bmin
	return answer
t = int(input())
ans = ''
for i in range(t):
	ans += str(ft()) + '\n'
print(ans, end='')


t = int(input())
for p in range(t):
	n = int(input())
	(m1, m2) = (0, 10 ** 9)
	for i in range(n):
		(x1, x2) = map(int, input().split())
		m1 = max(m1, x1)
		m2 = min(m2, x2)
	ans = m1 - m2
	if ans < 0:
		ans = 0
	print(ans)


import math
t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for j in range(n):
		arr.append(list(map(int, input().split())))
	arr = sorted(arr)
	l = math.inf
	r = 0
	for j in range(len(arr)):
		l = min(arr[j][1], l)
		r = max(arr[j][0], r)
	print(max(0, r - l))


from operator import itemgetter
test_count = int(input())
for _ in range(test_count):
	n = int(input())
	seg = list()
	for _ in range(n):
		(a, b) = map(int, input().split())
		seg.append((a, -1))
		seg.append((b, 1))
	seg.sort(key=itemgetter(0, 1))
	left = -1
	right = -1
	for s in seg:
		if s[1] == 1:
			left = s[0]
			break
	for s in reversed(seg):
		if s[1] == -1:
			right = s[0]
			break
	print(max(right - left, 0))


for _ in range(int(input())):
	n = int(input())
	minr = 10 ** 10
	maxl = -1
	for j in range(n):
		(x, y) = map(int, input().split())
		maxl = max(x, maxl)
		minr = min(minr, y)
	print(max(0, maxl - minr))


t = int(input())
for i in range(t):
	n = int(input())
	x = []
	y = []
	for i in range(n):
		(a, b) = map(int, input().split())
		x.append(a)
		y.append(b)
	if n == 1:
		print(0)
	elif min(y) > max(x):
		print(0)
	else:
		print(abs(max(x) - min(y)))


from sys import stdin, stdout, setrecursionlimit
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	print(max(max(a) - min(b), 0))


t = int(input())
l = []
for i in range(t):
	n = int(input())
	if n == 1:
		p = input().split()
		l.append(0)
	else:
		r1 = []
		r2 = []
		for i in range(n):
			p = input().split()
			r1.append(int(p[0]))
			r2.append(int(p[1]))
		r1 = max(r1)
		r2 = min(r2)
		if r1 - r2 <= 0:
			l.append(0)
		else:
			l.append(r1 - r2)
for i in l:
	print(i)


t = int(input())
for i in range(t):
	n = int(input())
	nach = []
	kon = []
	for j in range(n):
		p = list(map(int, input().split()))
		nach.append(p[0])
		kon.append(p[1])
	nach.sort()
	kon.sort()
	if nach[n - 1] > kon[0]:
		print(nach[n - 1] - kon[0])
	else:
		print(0)


for z in range(int(input())):
	n = int(input())
	f = 10000000000.0
	s = 0
	for i in range(n):
		(x, y) = map(int, input().split())
		f = min(f, y)
		s = max(s, x)
	if n == 1:
		print(0)
	elif s - f >= 0:
		print(s - f)
	else:
		print(0)


n = int(input())
for i in range(n):
	k = int(input())
	minn = 10 ** 10
	maxx = 0
	for i in range(k):
		(l, r) = map(int, input().split())
		minn = min(minn, r)
		maxx = max(maxx, l)
	print(max(0, maxx - minn))


t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for k in range(n):
		arr.append(tuple(map(int, input().split())))
	(l_max, r_min) = (arr[0][1], arr[0][0])
	for (l, r) in arr:
		if l_max > r:
			l_max = r
		if r_min < l:
			r_min = l
	print(max(0, r_min - l_max))


a = int(input())
for i in range(a):
	b = int(input())
	minRight = 10000000001
	maxLeft = -1
	for j in range(b):
		c = input().split()
		left = int(c[0])
		right = int(c[1])
		if left > maxLeft:
			maxLeft = left
		if right < minRight:
			minRight = right
	if minRight < maxLeft:
		print(maxLeft - minRight)
	else:
		print(0)


cn = int(input())
m = 0
n = 10 ** 9
for i in range(cn):
	co = int(input())
	for i in range(co):
		(x, y) = map(int, input().split())
		if m <= x:
			m = x
		if n >= y:
			n = y
	if n < m:
		print(m - n)
	else:
		print(0)
	m = 0
	n = 10 ** 9


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	N = []
	min1 = 1000000001
	max0 = -1
	for j in range(n):
		a = list(map(int, input().split(' ')))
		if a[0] > max0 or max0 == -1:
			max0 = a[0]
		if a[1] < min1:
			min1 = a[1]
	if min1 >= max0:
		ans.append(0)
	else:
		ans.append(max0 - min1)
for i in ans:
	print(i)


def solve(maxarray, minarray):
	mn = min(minarray)
	mx = max(maxarray)
	if mx - mn <= 0:
		result = 0
	else:
		result = mx - mn
	return result
t = int(input())
res = []
for i in range(t):
	n = int(input())
	minarray = []
	maxarray = []
	for i in range(n):
		inp = input().split()
		l = int(inp[0])
		r = int(inp[1])
		maxarray.append(l)
		minarray.append(r)
	res.append(solve(maxarray, minarray))
print('\n'.join(map(str, res)))


test = int(input())
for _ in range(test):
	n = int(input())
	mas = 0
	minA = 10 ** 10
	maxB = -1
	for __ in range(n):
		mas = list(map(int, input().split()))
		minA = min([minA, mas[1]])
		maxB = max([maxB, mas[0]])
	if maxB > minA:
		print(maxB - minA)
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	(k, m) = map(int, input().split())
	mn = m
	mx = k
	for j in range(n - 1):
		(k, m) = map(int, input().split())
		if m < mn:
			mn = m
		if k > mx:
			mx = k
	if n < 2:
		print(0)
	elif mx - mn < 0:
		print(0)
	else:
		print(mx - mn)


t = int(input())
for i in range(t):
	n = int(input())
	a = 1000000001
	b = 0
	for j in range(n):
		(l, r) = map(int, input().split())
		if l >= b:
			b = l
		if r <= a:
			a = r
	if b <= a:
		print(0)
	else:
		print(b - a)


for _ in range(int(input())):
	n = int(input())
	(a, b) = (10000000000.0, 0)
	while n:
		n -= 1
		(l, r) = map(int, input().split())
		a = min(a, r)
		b = max(b, l)
	print(max(0, b - a))


for _ in range(int(input())):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(a1, b1) = map(int, input().split())
		a.append(a1)
		b.append(b1)
	print(max(0, max(a) - min(b)))


ll = lambda : map(int, input().split())
t = lambda : int(input())
ss = lambda : input()
lx = lambda x: map(int, input().split(x))
yy = lambda : print('YES')
nn = lambda : print('NO')
from math import log10, log2, ceil, factorial as fac, gcd, inf
for _ in range(t()):
	(a, b) = (inf, 0)
	for i in range(t()):
		(n, m) = ll()
		a = min(a, m)
		b = max(b, n)
	print(b - a if b - a > 0 else 0)


for _ in [0] * int(input()):
	n = int(input())
	a = [[*map(int, input().split())] for _ in [0] * n]
	(x, y) = [min((x[1] for x in a)), max((x[0] for x in a))]
	print(max(0, y - x))


for i in range(int(input())):
	mine = 10000000000.0
	maxb = 0
	for j in range(int(input())):
		(a, b) = map(int, input().split())
		mine = min(mine, b)
		maxb = max(maxb, a)
	if maxb < mine:
		print(0)
	else:
		print(maxb - mine)


l = []
x = []
r = []
s = 0
a = int(input())
for i in range(a):
	b = int(input())
	for ii in range(b):
		x = list(map(int, input().split()))
		l.append(x[0])
		r.append(x[1])
	s = max(l) - min(r)
	l = []
	r = []
	if s < 0:
		print(0)
	else:
		print(s)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	minr = sys.maxsize
	maxl = 0
	for j in range(n):
		(a, b) = map(int, input().split())
		minr = min(b, minr)
		maxl = max(a, maxl)
	if maxl < minr:
		print(0)
	else:
		print(abs(minr - maxl))


for __ in range(int(input())):
	n = int(input())
	ar = []
	for ________ in range(n):
		ar.append(list(map(int, input().split())))
	ar.sort(key=lambda x: x[1])
	num1 = ar[0][1]
	ar.sort(key=lambda x: -x[0])
	num2 = ar[0][0]
	print(max(0, num2 - num1))


t = int(input())
for i in range(t):
	n = int(input())
	l = []
	for k in range(n):
		(x, y) = map(int, input().split())
		l.append((x, y))
	if n == 1:
		print(0)
		continue
	left = l[0][0]
	right = l[0][1]
	for seg in l[1:]:
		left = max(seg[0], left)
		right = min(seg[1], right)
	print(max(0, left - right))


for _ in range(int(input())):
	am = int(input())
	lmax = -99999
	rmin = 999999999999999
	for i in range(am):
		(a, b) = map(int, input().split())
		lmax = max(a, lmax)
		rmin = min(rmin, b)
	if am == 1:
		print(0)
		continue
	print(max(lmax - rmin, 0))


Max = 0
Min = 1000000000000
otv = ''
t = int(input())
for i in range(1, t + 1):
	n = int(input())
	if n > 1:
		for j in range(1, n + 1):
			(l, p) = map(int, input().split())
			if p < Min:
				Min = p
			if l > Max:
				Max = l
		if Max - Min > 0:
			otv = otv + '\n' + str(Max - Min)
		else:
			otv = otv + '\n' + '0'
	else:
		(l, p) = map(int, input().split())
		otv = otv + '\n' + '0'
	Max = 0
	Min = 1000000000000
print(otv)


for _ in range(int(input())):
	n = int(input())
	l1 = []
	l2 = []
	for i in range(n):
		(a, b) = map(int, input().split())
		l1.append(a)
		l2.append(b)
	if n == 1:
		print(0)
	elif max(l1) > min(l2):
		print(max(l1) - min(l2))
	else:
		print(0)


for _ in range(int(input())):
	(min_, max_) = (10 ** 9, 0)
	shit = int(input())
	for __ in range(shit):
		(a, b) = [int(___) for ___ in input().split()]
		if b < min_:
			min_ = b
		if a > max_:
			max_ = a
	if shit == 1:
		print(0)
		continue
	if max_ - min_ <= 0:
		print(0)
	else:
		print(max_ - min_)


res = list()
for j in range(int(input())):
	n = int(input())
	ps = list()
	for i in range(n):
		psi = list(map(int, input().split()))
		ps.append((psi[0], psi[1]))
	if n == 1:
		res.append(0)
	else:
		psend = list(ps)
		psend.sort(key=lambda e: e[1])
		ps.sort(key=lambda e: e[0])
		a = ps[-1][0] - psend[0][1]
		if a < 0:
			res.append(0)
		else:
			res.append(a)
for ans in res:
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	ansl = 0
	ansr = 10 ** 9
	for j in range(n):
		(b, e) = map(int, input().split())
		ansl = max(ansl, b)
		ansr = min(ansr, e)
	print(max(ansl - ansr, 0))


t = int(input())
l = 0
r = 0
l1 = []
r1 = []
count = 0
x = 0
for i in range(t):
	n = int(input())
	for j in range(n):
		if n == 1:
			(aaa, zzz) = map(int, input().split())
			print(0)
			break
		(l, r) = map(int, input().split())
		l1.append(l)
		r1.append(r)
	if n != 1:
		if max(l1) > min(r1):
			print(max(l1) - min(r1))
		else:
			print(0)
	l1 = []
	r1 = []


t = int(input())
L = []
R = []
S = []
for i in range(t):
	n = int(input())
	for k in range(n):
		(l, r) = map(int, input().split())
		R.append(r)
		L.append(l)
	if n != 1:
		s = max(L) - min(R)
		if s < 0:
			S.append(0)
		else:
			S.append(s)
	else:
		S.append(0)
	R = []
	L = []
for i in range(t):
	print(S[i])


t = int(input())
ans = [0] * t
for j in range(t):
	n = int(input())
	(a, b) = map(int, input().split())
	r = []
	l = []
	r.append(b)
	l.append(a)
	x = a
	y = b
	for i in range(n - 1):
		(a, b) = map(int, input().split())
		r.append(b)
		l.append(a)
		if a > x:
			x = a
		if b < y:
			y = b
	k = True
	if x > y:
		(x, y) = (y, x)
	for i in range(n):
		if y <= r[i] and x >= l[i]:
			continue
		else:
			k = False
			break
	if k:
		print(0)
	else:
		print(abs(y - x))


N = int(input())
a = []
for i in range(N):
	m = int(input())
	maxmin = 0
	minmax = 0
	for j in range(m):
		x = input()
		x = x.split()
		x = [int(x[0]), int(x[1])]
		if maxmin == 0 and minmax == 0:
			maxmin = x[1]
			minmax = x[0]
		else:
			if x[0] > minmax:
				minmax = x[0]
			if x[1] < maxmin:
				maxmin = x[1]
	if minmax - maxmin < 0:
		y = 0
	else:
		y = minmax - maxmin
	a.append(y)
for i in a:
	print(i)


for _ in range(int(input())):
	first = True
	(b, a) = (0, 1000000001)
	for i in range(int(input())):
		(l, r) = map(int, input().split())
		if r < a:
			a = r
		if l > b:
			b = l
	print(max(0, b - a))


t = int(input())
otrmin = []
otrmax = []
for i in range(0, t):
	n = int(input())
	for i in range(n):
		a = input().split(' ')
		otrmin.append(int(a[0]))
		otrmax.append(int(a[1]))
	a = max(otrmin)
	b = min(otrmax)
	otrmin = []
	otrmax = []
	if a > b:
		print(a - b)
	else:
		print(0)


def get_lines():
	out = []
	for c in range(int(input())):
		out.append([int(a) for a in input().split()])
	return out

def get_min_max(lines):
	minimum = float('inf')
	maximum = float('-inf')
	for c in lines:
		if c[1] < minimum:
			minimum = c[1]
		if c[0] > maximum:
			maximum = c[0]
	return [minimum, maximum]
for c in range(int(input())):
	(a, b) = get_min_max(get_lines())
	if b <= a:
		print(0)
	else:
		print(b - a)


q = int(input())
for i in range(q):
	k = int(input())
	r = float('inf')
	l = float('-inf')
	for j in range(k):
		(x, y) = map(int, input().split())
		l = max(l, x)
		r = min(r, y)
	if l - r < 0:
		print(0)
	else:
		print(l - r)


for i in range(int(input())):
	n = int(input())
	a = []
	b = []
	for j in range(n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	s = max(a) - min(b)
	if s < 0:
		print(0)
	else:
		print(s)


n = int(input())
for i in range(n):
	(mnl, mxr) = (-1, -1)
	m = int(input())
	mass = []
	for g in range(m):
		(a, b) = map(int, input().split())
		if mnl == -1 or b < mnl:
			mnl = b
		if mxr == -1 or a > mxr:
			mxr = a
		was = False
		for j in range(len(mass)):
			k = mass[j]
			if k[0] <= a <= k[1] or k[0] <= b <= k[1] or a <= k[0] <= b or (a <= k[1] <= b):
				mass[j] = (max(a, k[0]), min(b, k[1]))
				was = True
				break
		if not was:
			mass.append((a, b))
	if len(mass) == 1:
		print(0)
	else:
		print(abs(mxr - mnl))


t = int(input())
s = []
for j in range(t):
	n = int(input())
	b = []
	c = []
	d = []
	for i in range(n):
		a = list(map(int, input().split()))
		b.append(a[0])
		b.append(a[1])
	for i in range(n * 2):
		if i % 2 == 0:
			c.append(b[i])
		else:
			d.append(b[i])
	e1 = max(c)
	e2 = min(d)
	if e1 - e2 >= 0:
		s.append(e1 - e2)
	else:
		s.append(0)
for i in range(len(s)):
	print(s[i])


t = int(input())
for _ in range(t):
	n = int(input())
	lines = [0] * n
	liness = [0] * n
	if n > 1:
		for i in range(n):
			inp = tuple(map(int, input().split()))
			lines[i] = inp[0]
			liness[i] = inp[1]
		lines.sort()
		liness.sort()
		res = lines[-1] - liness[0]
		if res > 0:
			print(res)
		else:
			print(0)
	else:
		inp = tuple(map(int, input().split()))
		print(0)


for i in range(int(input())):
	n = int(input())
	(ma, mi) = map(int, input().split())
	for j in range(1, n):
		(l, r) = map(int, input().split())
		if l > ma:
			ma = l
		if r < mi:
			mi = r
	if ma - mi < 0:
		print(0)
	else:
		print(ma - mi)


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	a.sort()
	b.sort()
	print(max(a[n - 1] - b[0], 0))


t = int(input())
ans = [0] * t
for i in range(t):
	n = int(input())
	(a, b) = map(int, input().split())
	Mr = b
	Ml = a
	for j in range(1, n):
		(L, R) = map(int, input().split())
		Ml = max(Ml, L)
		Mr = min(Mr, R)
	if Ml > Mr:
		ans[i] = Ml - Mr
for i in range(t):
	print(ans[i])


for i in range(int(input())):
	l = []
	r = []
	for j in range(int(input())):
		(li, ri) = map(int, input().split())
		l.append(li)
		r.append(ri)
	print(max(0, max(l) - min(r)))


def read():
	n = int(input())
	a = []
	for i in range(n):
		(start, end) = map(int, input().split())
		a.append((start, end))
	return (n, a)

def solve(n, a):
	last_start = max(a, key=lambda ai: ai[0])[0]
	first_end = min(a, key=lambda ai: ai[1])[1]
	if last_start > first_end:
		return last_start - first_end
	else:
		return 0
for i in range(int(input())):
	result = solve(*read())
	print(result)


t = int(input())
arr = []
for i in range(t):
	n = int(input())
	mxl = 0
	mnr = 1000000000.0
	for j in range(n):
		(l, r) = map(int, input().split())
		mxl = max(l, mxl)
		mnr = min(r, mnr)
	if mnr - mxl <= 0:
		arr.append(abs(mnr - mxl))
	else:
		arr.append(0)
[print(i) for i in arr]


t = int(input())
for j in range(t):
	n = int(input())
	l_max = -1
	r_min = 10 ** 9 + 1
	for i in range(n):
		(l, r) = [int(x) for x in input().split()]
		if l > l_max:
			l_max = l
		if r < r_min:
			r_min = r
	if l_max > r_min:
		print(l_max - r_min)
	else:
		print(0)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		input()
		print(0)
		continue
	ot = []
	for j in range(n):
		otz = list(map(int, input().split()))
		ot += [otz]
	mn = min([h[1] for h in ot])
	mx = max([h[0] for h in ot])
	res = mx - mn
	if res < 0:
		res = 0
	print(res)


t = int(input())
for _ in range(t):
	lm = 0
	rm = 1000000001
	n = int(input())
	for i in range(n):
		(l, r) = map(int, input().split())
		if r < rm:
			rm = r
		if l > lm:
			lm = l
	if rm >= lm:
		print(0)
	else:
		print(lm - rm)


def read():
	n = int(input())
	a = []
	for i in range(n):
		(start, end) = map(int, input().split())
		a.append((start, end))
	return (n, a)

def solve(n, a):
	a.sort(key=lambda p: p[0], reverse=True)
	last_start = a[0][0]
	a.sort(key=lambda p: p[1])
	first_end = a[0][1]
	if last_start > first_end:
		return last_start - first_end
	else:
		return 0
for i in range(int(input())):
	result = solve(*read())
	print(result)


t = int(input())
for i in range(t):
	lm = 0
	rm = 10 ** 9 + 1
	n = int(input())
	for j in range(n):
		(l, r) = [int(x) for x in input().strip().split()]
		if l > lm:
			lm = l
		if r < rm:
			rm = r
	print(max(lm - rm, 0))


t = int(input())
for _ in range(t):
	n = int(input())
	minR = 10 ** 9
	maxL = 0
	for _ in range(n):
		(l, r) = map(int, input().split())
		if l > maxL:
			maxL = l
		if r < minR:
			minR = r
	if n == 1:
		print(0)
	else:
		print(maxL - minR if maxL - minR > 0 else 0)


t = int(input())
for i in range(t):
	n = int(input())
	(start, end) = (0, 100000000000.0)
	for j in range(n):
		(l, r) = map(int, input().split())
		if l > start:
			start = l
		if r < end:
			end = r
	if start > end:
		print(start - end)
	else:
		print(0)


number_of_sets = int(input())
sets = []
for i in range(number_of_sets):
	temp = []
	numbers_of_linesegments = int(input())
	for k in range(numbers_of_linesegments):
		temp.append(tuple(map(int, input().split())))
	sets.append(temp)
for elem in sets:
	start = -1
	end = 10000000000
	for object in elem:
		if object[0] > start:
			start = object[0]
		if object[1] < end:
			end = object[1]
	if end > start:
		print(0)
	else:
		print(start - end)


t = int(input())
res = []
for _ in range(t):
	n = int(input())
	mr = 10 ** 9 + 1
	ml = 0
	for i in range(n):
		(l, r) = map(int, input().split())
		if r < mr:
			mr = r
		if l > ml:
			ml = l
	dif = ml - mr
	if dif <= 0:
		res.append(0)
	else:
		res.append(dif)
print(*res, sep='\n')


MAXN = 10 ** 9 + 1
for t in range(int(input())):
	n = int(input())
	min = MAXN
	max = 0
	for j in range(n):
		[a, b] = map(int, input().split())
		if b < min:
			min = b
		if a > max:
			max = a
	if min >= max:
		print(0)
	else:
		print(max - min)


t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	(a, b) = ([], [])
	for i in range(n):
		(a1, a2) = map(int, input().split(' '))
		a.append(a2)
		b.append(a1)
	if max(b) - min(a) <= 0:
		print('0')
	else:
		print(max(b) - min(a))


t = int(input())
for i in range(t):
	n = int(input())
	l = [0] * n
	r = [0] * n
	naim = 10 ** 9
	naib = 0
	for j in range(n):
		(l[j], r[j]) = map(int, input().split())
		naim = min(naim, r[j])
		naib = max(naib, l[j])
	k = naib - naim
	if k > 0:
		print(k)
	else:
		print(0)


sets = int(input())
result = []
for i in range(sets):
	lines_count = int(input())
	lines = []
	for j in range(lines_count):
		lines.append(list(map(int, input().split(' '))))
	lines = sorted(lines, key=lambda x: x[0])
	end_lines = sorted(lines, key=lambda x: x[1])
	res = lines[lines_count - 1][0] - end_lines[0][1] if lines[lines_count - 1][0] - end_lines[0][1] >= 0 else 0
	result.append(res)
for i in result:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	l1 = []
	l2 = []
	for i in range(n):
		s = input().split()
		l1.append(int(s[0]))
		l2.append(int(s[1]))
	if max(l1) - min(l2) < 0:
		print(0)
	else:
		print(max(l1) - min(l2))


for _ in range(int(input())):
	n = int(input())
	lines = []
	for i in range(n):
		(l, r) = tuple((int(i) for i in input().split()))
		lines.append((l, r))
	if len(lines) == 1:
		print(0)
		continue
	lines.sort(key=lambda x: x[1])
	my_l = my_r = lines[0][1]
	lines.sort(key=lambda x: x[0])
	if lines[-1][0] > my_r:
		my_r = lines[-1][0]
	print(my_r - my_l)


t = int(input())
for i in range(t):
	n = int(input())
	mxl = -1
	mnr = 10 ** 10
	for j in range(n):
		(l, r) = map(int, input().split())
		mnr = min(mnr, r)
		mxl = max(mxl, l)
	if mxl - mnr < 0:
		print(0)
	else:
		print(mxl - mnr)


t = int(input())
for i in range(t):
	n = int(input())
	l = []
	r = []
	for j in range(n):
		(a, b) = list(map(int, input().split()))
		l.append(a)
		r.append(b)
	if max(l) - min(r) < 0:
		print(0)
	else:
		print(max(l) - min(r))


t = int(input())
for w in range(t):
	n = int(input())
	otrezki = []
	minR = int(1000000000.0 + 7)
	maxL = 0
	for i in range(n):
		a = [int(i) for i in input().split()]
		minR = min(minR, a[1])
		maxL = max(a[0], maxL)
	print(max(0, maxL - minR))


t = int(input())
for g in range(0, t):
	n = int(input())
	max0 = 0
	min1 = 1000000000
	for i in range(0, n):
		a = list(map(int, input().split()))
		if a[0] > max0:
			max0 = a[0]
		if min1 > a[1]:
			min1 = a[1]
	if max0 - min1 < 0:
		print(0)
	else:
		print(max0 - min1)


import itertools

def main():
	n = int(input())
	for j in range(n):
		d = int(input())
		mis = []
		mxs = []
		for i in range(d):
			(a, b) = [int(v) for v in input().split()]
			mis.append(b)
			mxs.append(a)
		(a, b) = (min(mis), max(mxs))
		if a <= b:
			print(b - a)
		else:
			print(0)
main()


import sys, itertools, math

def ia():
	return [int(i) for i in sys.stdin.readline().strip().split(' ')]

def ii():
	return int(sys.stdin.readline().strip())

def istr():
	return sys.stdin.readline().strip()
for _ in range(ii()):
	S = []
	E = []
	for _ in range(ii()):
		(s, e) = ia()
		S.append(s)
		E.append(e)
	print(max(0, max(S) - min(E)))


t = int(input())
result = []
for i in range(t):
	n = int(input())
	a1 = []
	a2 = []
	for j in range(n):
		b = list(map(int, input().split()))
		a1.append(b[0])
		a2.append(b[1])
	if max(a1) > min(a2):
		res = max(a1) - min(a2)
	else:
		res = 0
	result.append(res)
for i in result:
	print(i)


import os
import sys
DEBUG = 'DEBUG' in os.environ

def inp():
	return sys.stdin.readline().rstrip()

def Rint():
	return int(inp())

def Rints():
	return [int(e) for e in inp().split()]

def main():
	n = Rint()
	res = [None, None]
	(starts, ends) = ([], [])
	for _ in range(n):
		seg = Rints()
		starts.append(seg[0])
		ends.append(seg[1])
	res[0] = max(starts)
	res[1] = min(ends)
	if res[1] < res[0]:
		return res[0] - res[1]
	else:
		return 0
for _ in range(Rint()):
	print(main())


n = int(input())
for _ in range(n):
	lst = []
	n = int(input())
	first_input = input().split()
	min_end = int(first_input[1])
	max_start = int(first_input[0])
	for _ in range(n - 1):
		i = input().split()
		max_start = max(max_start, int(i[0]))
		min_end = min(min_end, int(i[1]))
	if n == 1 or max_start - min_end < 0:
		print(0)
	else:
		print(max_start - min_end)


k = int(input())
for i in range(k):
	n = int(input())
	mi = 1000000001
	ma = 0
	for j in range(n):
		(a, b) = map(int, input().split())
		if b < mi:
			mi = b
		if a > ma:
			ma = a
	if n == 1 or ma < mi:
		print(0)
	else:
		print(ma - mi)


t = int(input())
for i in range(t):
	n = int(input())
	(l, r) = (1, 1000000000)
	for i in range(n):
		tmp = list(map(int, input().split()))
		l = max(l, tmp[0])
		r = min(r, tmp[1])
	print(max(r, l) - r)


t = int(input())
for p in range(t):
	n = int(input())
	l = r = k = 0
	b = False
	if n < 2:
		(l, r) = map(int, input().split())
		print(0)
	else:
		for i in range(n):
			(li, ri) = map(int, input().split())
			if l == 0 and r == 0:
				l = li
				r = ri
			elif ri >= l and li <= r:
				k += 1
				if b == False:
					if ri < r:
						r = ri
					if li > l:
						l = li
			elif ri > l and li > r:
				if b == False:
					l = r
				r = li
				b = True
			elif ri < l:
				if b == False:
					r = l
				l = ri
				b = True
		if k < n - 1:
			print(r - l)
		else:
			print(0)


for _ in range(int(input())):
	n = int(input())
	a = 10 ** 9
	b = 0
	for i in range(n):
		(t, l) = map(int, input().split())
		b = max(b, t)
		a = min(a, l)
	print(max(0, b - a))


t = int(input())
for i in range(t):
	n = int(input())
	leviy_otr = [10000000000, 10000000000]
	pryavi_otr = [-10000000000, -10000000000]
	for j in range(n):
		a = input().split()
		a = [int(a[0]), int(a[1])]
		if leviy_otr[1] > a[1]:
			leviy_otr = a
		if pryavi_otr[0] < a[0]:
			pryavi_otr = a
	print(max(0, pryavi_otr[0] - leviy_otr[1]))