#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define nl '\n'
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K th element in a set (counting from zero)
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, h;
cin >> n >> h;
vector<ll> v(n, n);
vector<ll> pr(n + 1, 0);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
x--;
pr[x]++;
pr[y]--;
}
for (int i = 1; i < n; i++)
pr[i] += pr[i - 1];
for (int i = 0; i < n; i++) {
v[i] -= pr[i];
}
sort(v.begin(), v.end());
ll sum = 0;
for (int i = 0; i < h; i++)
sum += v[i];
cout << sum << nl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+Cgp1c2luZyBuYW1lc3BhY2UgX19nbnVfcGJkczsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBubCAnXG4nCiNkZWZpbmUgb3JkZXJlZF9zZXQgdHJlZTxsbCwgbnVsbF90eXBlLGxlc3NfZXF1YWw8bGw+LCByYl90cmVlX3RhZyx0cmVlX29yZGVyX3N0YXRpc3RpY3Nfbm9kZV91cGRhdGU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Ci8vb3JkZXJfb2Zfa2V5IChrKSA6IE51bWJlciBvZiBpdGVtcyBzdHJpY3RseSBzbWFsbGVyIHRoYW4gayAuCi8vZmluZF9ieV9vcmRlcihrKSA6IEsgdGggZWxlbWVudCBpbiBhIHNldCAoY291bnRpbmcgZnJvbSB6ZXJvKQoKaW50IG1haW4oKQp7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwogICAgY291dC50aWUoTlVMTCk7CgogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKCiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG4sIGg7CiAgICAgICAgY2luID4+IG4gPj4gaDsKCiAgICAgICAgdmVjdG9yPGxsPiB2KG4sIG4pOwogICAgICAgIHZlY3RvcjxsbD4gcHIobiArIDEsIDApOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGludCB4LCB5OwogICAgICAgICAgICBjaW4gPj4geCA+PiB5OwogICAgICAgICAgICB4LS07CiAgICAgICAgICAgIHByW3hdKys7CiAgICAgICAgICAgIHByW3ldLS07CgogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IG47IGkrKykKICAgICAgICAgICAgcHJbaV0gKz0gcHJbaSAtIDFdOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICB2W2ldIC09IHByW2ldOwogICAgICAgIH0KCiAgICAgICAgc29ydCh2LmJlZ2luKCksIHYuZW5kKCkpOwoKICAgICAgICBsbCBzdW0gPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgaDsgaSsrKQogICAgICAgICAgICBzdW0gKz0gdltpXTsKCiAgICAgICAgY291dCA8PCBzdW0gPDwgbmw7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K