#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printf(char *, int);
int query_tree(int *, int *, int, int /*<<< orphan*/, int, int, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int /*<<< orphan*/ update_tree(int *, int *, int, int /*<<< orphan*/, int, int,
                               int, int);
int main(void) {
  int t, li, hi;
  scanf("%d", &t);
  int a;
  for (a = 0; a < t; a++) {
    int g, h;
    scanf("%d %d", &g, &h);
    int i;
    int *tree = (int *)calloc(10000000, 8);
    int *lazy = (int *)calloc(10000000, 8);
    int *arr = (int *)calloc(g, 8);
    for (i = 0; i < g; i++) {
      scanf("%d%d", &li, &hi);
      update_tree(tree, lazy, 1, 0, g - 1, li, hi, 1);
    }
    int min = g * h - query_tree(tree, lazy, 1, 0, g - 1, 0, h - 1);
    for (i = 1; i < g - h + 1; i++) {
      int y = g * h - query_tree(tree, lazy, 1, 0, g - 1, i, i + h - 1);
      if (y < min)
        min = y;
    }
    printf("%d\n", min);
    free(tree);
    free(lazy);
    free(arr);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8d(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1070 <scanf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    133b <main+0x1db>
lea    0xe65(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
mov    $0x0,%al
call   1070 <scanf@plt>
mov    $0x989680,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    $0x989680,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1246 <main+0xe6>
lea    0xe0b(%rip),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1070 <scanf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%r8d
sub    $0x1,%r8d
mov    -0xc(%rbp),%r9d
mov    -0x10(%rbp),%eax
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %eax,(%rsp)
movl   $0x1,0x8(%rsp)
call   1060 <update_tree@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11e8 <main+0x88>
mov    -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%r8d
sub    $0x1,%r8d
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    $0x1,%edx
xor    %r9d,%r9d
mov    %r9d,%ecx
mov    %eax,(%rsp)
call   1080 <query_tree@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1303 <main+0x1a3>
mov    -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%r8d
sub    $0x1,%r8d
mov    -0x20(%rbp),%r9d
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %eax,(%rsp)
call   1080 <query_tree@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    12f0 <main+0x190>
mov    -0x40(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    12f5 <main+0x195>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    128a <main+0x12a>
mov    -0x3c(%rbp),%esi
lea    0xcfe(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1188 <main+0x28>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xe8e(%rip),%rdi
lea    0x24(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
cmpl   $0x0,0x24(%rsp)
jle    133a <main+0x1da>
lea    0xe73(%rip),%r14
lea    0x2c(%rsp),%rbp
lea    0x20(%rsp),%r13
xor    %eax,%eax
jmp    11f6 <main+0x96>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0xe54(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x30(%rsp),%rdi
call   1030 <free@plt>
mov    0x28(%rsp),%eax
add    $0x1,%eax
cmp    0x24(%rsp),%eax
lea    0xe1b(%rip),%r14
lea    0x20(%rsp),%r13
jge    133a <main+0x1da>
mov    %eax,0x28(%rsp)
lea    0xdff(%rip),%rdi
lea    0x18(%rsp),%rsi
lea    0x1c(%rsp),%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
mov    $0x989680,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
mov    $0x989680,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
mov    0x18(%rsp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,0x30(%rsp)
mov    0x18(%rsp),%r8d
test   %r8d,%r8d
jle    12ac <main+0x14c>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0x18(%rsp),%r8d
add    $0xffffffff,%r8d
mov    0x2c(%rsp),%r9d
mov    0x20(%rsp),%eax
mov    %eax,(%rsp)
movl   $0x1,0x8(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <update_tree@plt>
add    $0x1,%ebx
mov    0x18(%rsp),%r8d
cmp    %r8d,%ebx
jl     1260 <main+0x100>
mov    0x1c(%rsp),%eax
mov    %eax,%r14d
imul   %r8d,%r14d
add    $0xffffffff,%r8d
add    $0xffffffff,%eax
mov    %eax,(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1080 <query_tree@plt>
sub    %eax,%r14d
mov    0x18(%rsp),%r8d
mov    0x1c(%rsp),%eax
cmp    %eax,%r8d
jle    11b0 <main+0x50>
xor    %r13d,%r13d
xchg   %ax,%ax
mov    %eax,%ebx
imul   %r8d,%ebx
add    $0xffffffff,%r8d
add    %r13d,%eax
add    $0x1,%r13d
mov    %eax,(%rsp)
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
mov    %r13d,%r9d
call   1080 <query_tree@plt>
sub    %eax,%ebx
cmp    %r14d,%ebx
cmovl  %ebx,%r14d
mov    0x18(%rsp),%r8d
mov    0x1c(%rsp),%eax
mov    %r8d,%ecx
sub    %eax,%ecx
cmp    %ecx,%r13d
jl     12f0 <main+0x190>
jmp    11b0 <main+0x50>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [5 x i8] c"%d%d\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %1
%14 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
store i32 0, i32* %5
br label %15
15: ; preds = %96, %0
%16 = load i32, i32* %5
%17 = load i32, i32* %2
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %99
19: ; preds = %15
%20 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %6, i32* %7)
%21 = call i64 @calloc(i32 10000000, i32 8)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %9
%23 = call i64 @calloc(i32 10000000, i32 8)
%24 = inttoptr i64 %23 to i32*
store i32* %24, i32** %10
%25 = load i32, i32* %6
%26 = call i64 @calloc(i32 %25, i32 8)
%27 = inttoptr i64 %26 to i32*
store i32* %27, i32** %11
store i32 0, i32* %8
br label %28
28: ; preds = %41, %19
%29 = load i32, i32* %8
%30 = load i32, i32* %6
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %44
32: ; preds = %28
%33 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4)
%34 = load i32*, i32** %9
%35 = load i32*, i32** %10
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, 1
%38 = load i32, i32* %3
%39 = load i32, i32* %4
%40 = call i32 @update_tree(i32* %34, i32* %35, i32 1, i32 0, i32 %37, i32 %38, i32 %39, i32 1)
br label %41
41: ; preds = %32
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %28
44: ; preds = %28
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = mul nsw i32 %45, %46
%48 = load i32*, i32** %9
%49 = load i32*, i32** %10
%50 = load i32, i32* %6
%51 = sub nsw i32 %50, 1
%52 = load i32, i32* %7
%53 = sub nsw i32 %52, 1
%54 = call i32 @query_tree(i32* %48, i32* %49, i32 1, i32 0, i32 %51, i32 0, i32 %53)
%55 = sub nsw i32 %47, %54
store i32 %55, i32* %12
store i32 1, i32* %8
br label %56
56: ; preds = %84, %44
%57 = load i32, i32* %8
%58 = load i32, i32* %6
%59 = load i32, i32* %7
%60 = sub nsw i32 %58, %59
%61 = add nsw i32 %60, 1
%62 = icmp slt i32 %57, %61
br i1 %62, label %63, label %87
63: ; preds = %56
%64 = load i32, i32* %6
%65 = load i32, i32* %7
%66 = mul nsw i32 %64, %65
%67 = load i32*, i32** %9
%68 = load i32*, i32** %10
%69 = load i32, i32* %6
%70 = sub nsw i32 %69, 1
%71 = load i32, i32* %8
%72 = load i32, i32* %8
%73 = load i32, i32* %7
%74 = add nsw i32 %72, %73
%75 = sub nsw i32 %74, 1
%76 = call i32 @query_tree(i32* %67, i32* %68, i32 1, i32 0, i32 %70, i32 %71, i32 %75)
%77 = sub nsw i32 %66, %76
store i32 %77, i32* %13
%78 = load i32, i32* %13
%79 = load i32, i32* %12
%80 = icmp slt i32 %78, %79
br i1 %80, label %81, label %83
81: ; preds = %63
%82 = load i32, i32* %13
store i32 %82, i32* %12
br label %83
83: ; preds = %81, %63
br label %84
84: ; preds = %83
%85 = load i32, i32* %8
%86 = add nsw i32 %85, 1
store i32 %86, i32* %8
br label %56
87: ; preds = %56
%88 = load i32, i32* %12
%89 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %88)
%90 = load i32*, i32** %9
%91 = call i32 @free(i32* %90)
%92 = load i32*, i32** %10
%93 = call i32 @free(i32* %92)
%94 = load i32*, i32** %11
%95 = call i32 @free(i32* %94)
br label %96
96: ; preds = %87
%97 = load i32, i32* %5
%98 = add nsw i32 %97, 1
store i32 %98, i32* %5
br label %15
99: ; preds = %15
%100 = load i32, i32* %1
ret i32 %100
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [5 x i8] c"%d%d\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%10 = load i32, i32* %1
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %76
12: ; preds = %0
%13 = bitcast i32* %4 to i8*
%14 = bitcast i32* %5 to i8*
br label %15
15: ; preds = %12, %67
%16 = phi i32 [ 0, %12 ], [ %73, %67 ]
%17 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %5)
%18 = call i64 @calloc(i32 10000000, i32 8)
%19 = inttoptr i64 %18 to i32*
%20 = call i64 @calloc(i32 10000000, i32 8)
%21 = inttoptr i64 %20 to i32*
%22 = load i32, i32* %4
%23 = call i64 @calloc(i32 %22, i32 8)
%24 = inttoptr i64 %23 to i32*
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %15, %27
%28 = phi i32 [ %35, %27 ], [ 0, %15 ]
%29 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* %2, i32* %3)
%30 = load i32, i32* %4
%31 = add nsw i32 %30, -1
%32 = load i32, i32* %2
%33 = load i32, i32* %3
%34 = call i32 @update_tree(i32* %19, i32* %21, i32 1, i32 0, i32 %31, i32 %32, i32 %33, i32 1)
%35 = add nuw nsw i32 %28, 1
%36 = load i32, i32* %4
%37 = icmp slt i32 %35, %36
br i1 %37, label %27, label %38
38: ; preds = %27, %15
%39 = phi i32 [ %25, %15 ], [ %36, %27 ]
%40 = load i32, i32* %5
%41 = mul nsw i32 %40, %39
%42 = add nsw i32 %39, -1
%43 = add nsw i32 %40, -1
%44 = call i32 @query_tree(i32* %19, i32* %21, i32 1, i32 0, i32 %42, i32 0, i32 %43)
%45 = sub nsw i32 %41, %44
%46 = load i32, i32* %4
%47 = load i32, i32* %5
%48 = icmp sgt i32 %46, %47
br i1 %48, label %49, label %67
49: ; preds = %38, %49
%50 = phi i32 [ %64, %49 ], [ %47, %38 ]
%51 = phi i32 [ %63, %49 ], [ %46, %38 ]
%52 = phi i32 [ %61, %49 ], [ %45, %38 ]
%53 = phi i32 [ %62, %49 ], [ 1, %38 ]
%54 = mul nsw i32 %50, %51
%55 = add nsw i32 %51, -1
%56 = add nsw i32 %53, -1
%57 = add i32 %56, %50
%58 = call i32 @query_tree(i32* %19, i32* %21, i32 1, i32 0, i32 %55, i32 %53, i32 %57)
%59 = sub nsw i32 %54, %58
%60 = icmp slt i32 %59, %52
%61 = select i1 %60, i32 %59, i32 %52
%62 = add nuw nsw i32 %53, 1
%63 = load i32, i32* %4
%64 = load i32, i32* %5
%65 = sub nsw i32 %63, %64
%66 = icmp slt i32 %53, %65
br i1 %66, label %49, label %67
67: ; preds = %49, %38
%68 = phi i32 [ %45, %38 ], [ %61, %49 ]
%69 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %68)
%70 = call i32 @free(i32* %19)
%71 = call i32 @free(i32* %21)
%72 = call i32 @free(i32* %24)
%73 = add nuw nsw i32 %16, 1
%74 = load i32, i32* %1
%75 = icmp slt i32 %73, %74
br i1 %75, label %15, label %76
76: ; preds = %67, %0
ret i32 0
}