QOJ.ac

QOJ

Límite de tiempo: 1.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#14503. Wormhole

Estadísticas

In the distant galaxy, there is an organization called the "Stellar Domain Order Bureau," which is tasked with maintaining the stability of the universe. To protect the peace of the galaxy, the Stellar Domain Order Bureau must control the unstable regions hidden in space—wormholes.

The Stellar Domain Order Bureau has discovered $n$ wormholes. Each wormhole can be represented by a one-dimensional spatial coordinate interval $[l_i, r_i]$, meaning the $i$-th wormhole spans from $l_i$ to $r_i$.

The Stellar Domain Order Bureau needs to select a contiguous subsequence $[L, R]$ from the $n$ known wormholes and control the wormholes within this interval. To stably control these wormholes, they must divide them into at most $k$ groups, such that the wormhole intervals within the same group do not overlap. Formally, for any two wormholes $[l_i, r_i]$ and $[l_j, r_j]$ in the same group, it must satisfy $r_i < l_j$ or $r_j < l_i$.

The Stellar Domain Order Bureau wishes to control as many wormholes as possible. Please calculate the maximum length of the wormhole sequence $[L, R]$ they can choose (i.e., $R - L + 1$).

Input

The input contains multiple test cases. The first line contains a positive integer $T$ ($1 \le T \le 10^4$), representing the number of test cases.

For each test case: The first line contains two integers $n, k$ ($1 \le k \le n \le 2 \times 10^5$). The next $n$ lines each contain two integers $l_i, r_i$ ($1 \le l_i \le r_i \le n$), representing the coordinate range of the $i$-th wormhole.

It is guaranteed that the sum of $n$ over all $T$ test cases does not exceed $2 \times 10^5$.

Output

For each test case: Output a single integer representing the maximum $R - L + 1$.

Examples

Input 1

2
3 1
1 2
2 3
3 3
5 2
1 5
1 3
2 4
4 5
1 1

Output 1

1
4

Note

For the first test case: Obviously, only a wormhole sequence of length 1 can be chosen.

For the second test case: One can choose the wormhole sequence $[2, 5]$, divide the 2nd and 4th wormholes into one group, and the 3rd and 5th wormholes into another group. The length of this wormhole sequence is 4. Clearly, no solution of length 5 exists. Thus, the answer is 4.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.