This page is still under construction.

Parts of this page are still being built. What you see may change.

Artifact

Time limit1sMemory limit128 MB

Summary
Given n row intervals, choose k consecutive columns and pay the cost to extend each row's interval to cover them, minimizing total added tiles.
Level

Medium7 of 10

Topics
Prefix sum, Sliding window, Math
Solved
No attempts yet

Problem

While tidying up an old cellar, I found an object that looks like an ancient artifact: a huge chessboard made of thousands of equal, tiny cells.

The board has 2⋅1092 \cdot 10^9 columns, numbered from 11 to 2⋅1092 \cdot 10^9 from left to right. The vertical lines are numbered from 00 to 2⋅1092 \cdot 10^9, and vertical line ii is the boundary between column i−1i-1 and column ii. The board has nn rows.

In each row ii there is exactly one contiguous strip of gold tiles. This strip lies between vertical line aia_i and vertical line bib_i; that is, the cells in columns ai+1,ai+2,…,bia_i+1, a_i+2, \dots, b_i are all covered with gold tiles, and every other cell of the row is empty.

You may add gold tiles to widen each row's gold strip (after widening, each row's gold tiles must still form a single contiguous strip). Your goal is to choose kk consecutive columns and add tiles so that, in every row, all kk of those columns are gold — forming a vertical band of gold, kk columns wide, that is full from the top row to the bottom row.

Write a program that prints the minimum number of extra gold tiles you must buy.

Input

The first line contains an integer dd (1≤d≤1001 \le d \le 100), the number of tests, followed by dd test descriptions.

The first line of each test contains two integers nn (1≤n≤1051 \le n \le 10^5) and kk (1≤k≤2⋅1091 \le k \le 2 \cdot 10^9). The second line contains nn pairs of integers aia_i and bib_i (0≤ai<bi≤1090 \le a_i < b_i \le 10^9), the numbers of the two vertical lines between which the gold tiles of row ii are laid.

Output

For each test, print on its own line the minimum number of gold tiles you must buy.

Hint

The picture below illustrates an example. Black cells are the cells that held gold tiles from the start. Gray cells show the minimal set of cells on which new tiles must be placed so that, after widening the gold strips, 22 consecutive columns become full of gold tiles from top to bottom.

Examples5

  1. Example 1

    Input
    1
    3 2
    0 3 3 4 5 8
    
    Expected output
    5
    
  2. Example 2

    Input
    1
    1 1
    2 5
    
    Expected output
    0
    
  3. Example 3

    Input
    1
    1 3
    4 5
    
    Expected output
    2
    
  4. Example 4

    Input
    2
    2 1
    0 2 5 7
    1 1
    2 5
    
    Expected output
    4
    0
    
  5. Example 5

    Input
    2
    3 2
    0 3 3 4 5 8
    1 1
    2 5
    
    Expected output
    5
    0