Egor came to the Olympiad in Informatics. But the tasks turned out to be too difficult, so Egor chose to sleep instead of solving problems. But as we all know, everyone is very happy when someone is the first who solved the problem. 

There are N problems in the contest. The i-th of them was solved at minute L_{i} for the first time. This task will stop increasing the joy of everyone around at the minute R_{i} + 1 (that is, R_{i} is the last minute when the task increases the joy). In other words, the i-th task increases the joyfulness for everyone by 1 unit from the minute L_{i} to the minute R_{i}.

In the anime that Egor watched recently, there are various techniques that can make a person fall asleep. Egor knows techniques that will allow him to sleep for any minutes between A and B minutes, i.e. Egor can sleep for A minutes, A + 1 minutes, ..., B minutes. Egor can also combine techniques, i.e. he can choose techniques with different duration and then sleep the total time of all techniques.

Egor wants to combine techniques to wake up in the most joyful moment possible. Your task is to find the maximum joy in which Egor can wake up.

------ Input Format ------ 

- The first line of the input contains the number T - the number of test cases. The test cases then follow.
- The first line of each test case contains integers N, A, and B - the number of tasks and A, B values described in the statement.
- The next N lines give you a description of the problems. Each line contains two numbers L_{i}, R_{i} - a segment of joy for every task.

------ Output Format ------ 

For each test case, print the maximum level of joy at which Egor can wake up.

------ Constraints ------ 

$1 ≤ T ≤ 3 \cdot 10^{5}$
$1 ≤N ≤3 \cdot 10^{5}$
$1 ≤ A ≤ B ≤ 10^{9}$
$1 ≤ L_{i} ≤ R_{i} ≤ 10^{18}$
- Sum of $N$ over all test cases does not exceed $3 \cdot 10^{5}$

------ subtasks ------ 

Subtask $1$ (30 points): $1 ≤ L_{i} ≤ R_{i} ≤ 10^{9}$
Subtask $2$ (10 points): $A = 1$
Subtask $3$ (60 points): Original constraints

----- Sample Input 1 ------ 
3
3 1 5
1 5
3 9
6 9
3 7 54
54 228
228 1337
69 213
3 1 9
100 100
99 99
98 98

----- Sample Output 1 ------ 
2
2
0

----- explanation 1 ------ 
- Test case $1$: We can use techniques $1$ and $2$ to wake up at minute $1 + 2 = 3$. At that time, there are $2$ tasks that give Egor joy: task $1$ (which gives joy during the range $[1, 5]$), and task $2$ (which gives joy during the range $[3, 9]$). It can be proven that Egor cannot acquire joy from all $3$ tasks at the same time, so $2$ is the maximum joyfulness Egor can wake up to.
- Test case $2$: We can use techniques $15$, $22$, and $32$ to wake up at minute $15 + 22 + 32 = 69$. At that time. there are $2$ tasks that give Egor joy: task $1$ and task $3$.
- Test case $3$: The maximum minute we can wake up to is $1 + 2 + \dots + 9 = 45$, which means we cannot acquire joy from any task.

Difficulty: VERY_HARD
Tags: Sorting

Data structures

Range queries

Complete search
Skill types: Sorting

Data structures

Range queries

Complete search

Starter code:

Solutions:
def find(x, y):
	(l, r) = (1, b - a + 1)
	while l <= r:
		mid = (l + r) // 2
		n = mid + a - 1
		n = n * (n + 1) // 2 - u
		if n < x:
			m = b - mid
			m = v - m * (m + 1) // 2
			if m >= x:
				return True
			l = mid + 1
		if n >= x and n <= y:
			return True
		if n > y:
			r = mid - 1
	return False
for _ in range(int(input())):
	(n, a, b) = map(int, input().split())
	(l, r) = ([], [])
	for _ in range(n):
		(x, y) = map(int, input().split())
		l.append(x)
		r.append(y)
	l.sort()
	r.sort()
	ans = 0
	(u, v) = (a * (a - 1) // 2, b * (b + 1) // 2)
	(temp, j) = (0, 0)
	t = []
	for i in l:
		while i > r[j]:
			q = t.pop()
			if ans < temp:
				if find(q, r[j]):
					ans = temp
			j = j + 1
			temp = temp - 1
		temp = temp + 1
		t.append(i)
		if ans < temp:
			if find(i, r[j]):
				ans = temp
	while len(t):
		if ans < len(t):
			if find(t.pop(), r[j]):
				ans = len(t) + 1
			j = j + 1
			continue
		break
	print(ans)


import sys
readline = sys.stdin.readline
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))

def solve():
	(n, a, b) = nm()
	lr = [tuple(nm()) for _ in range(n)]
	event = [(l, 1) for (l, r) in lr] + [(r + 1, -1) for (l, r) in lr] + [(10 ** 20, 0)]
	event.sort()
	ans = cur = 0
	bef = 0
	for (t, v) in event:
		(l, r, c) = (bef, t - 1, cur)
		bef = t
		cur += v
		if ans >= c:
			continue
		ok = 0
		ng = b - a + 2
		while ng - ok > 1:
			mid = (ok + ng) // 2
			if (b + b - mid + 1) * mid // 2 < l:
				ok = mid
			else:
				ng = mid
		if ok < b - a + 1 and (a + a + ok) * (ok + 1) // 2 <= r:
			ans = c
	print(ans)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()