#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf = 4E18;
constexpr int N = 500005;
constexpr int V = 1E9 + 1;
int cnt = 2;
int L[25 * N], R[25 * N];
i64 sum[25 * N];
int be[25 * N], tag[25 * N];
void apply(int &p, int l, int r, int bq, int tq) {
if (p == 0) {
p = cnt++;
}
if (bq) {
be[p] = bq;
tag[p] = tq;
sum[p] = 1LL * (r - l) * tq;
}
}
void push(int p, int l, int r) {
int m = (l + r) / 2;
apply(L[p], l, m, be[p], tag[p]);
apply(R[p], m, r, be[p], tag[p]);
be[p] = tag[p] = 0;
}
void pull(int p) {
sum[p] = sum[L[p]] + sum[R[p]];
}
i64 rangeQuery(int p, int l, int r, int x, int y) {
if (x <= l && r <= y) {
return sum[p];
}
push(p, l, r);
int m = (l + r) / 2;
i64 sum = 0;
if (x < m) {
sum += rangeQuery(L[p], l, m, x, y);
}
if (m < y) {
sum += rangeQuery(R[p], m, r, x, y);
}
return sum;
}
i64 get(int x) {
if (x == -1) {
return 0;
}
return rangeQuery(1, 0, V, 0, x + 1);
}
int rangeApply(int p, int l, int r, int x, int y, int tb) {
if (x <= l && r <= y) {
apply(p, l, r, 1, tb);
return p;
}
push(p, l, r);
int m = (l + r) / 2;
if (x < m) {
L[p] = rangeApply(L[p], l, m, x, y, tb);
}
if (m < y) {
R[p] = rangeApply(R[p], m, r, x, y, tb);
}
pull(p);
return p;
}
int findFirst1(int p, int l, int r, i64 rest) {
if (rest - sum[p] <= 0) {
return -1;
}
if (l + 1 == r) {
return l;
}
push(p, l, r);
int m = (l + r) / 2;
int res = findFirst1(L[p], l, m, rest);
if (res == -1) {
res = findFirst1(R[p], m, r, rest - sum[L[p]]);
}
return res;
}
int findFirst2(int p, int l, int r, i64 rest, int cm) {
if (rest - sum[p] - 1LL * (r - l) * cm > 0) {
return -1;
}
if (l + 1 == r) {
return l;
}
push(p, l, r);
int m = (l + r) / 2;
int res = findFirst2(L[p], l, m, rest, cm);
if (res == -1) {
res = findFirst2(R[p], m, r, rest - sum[L[p]] - 1LL * (m - l) * cm, cm);
}
return res;
}
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i] >> b[i];
}
rangeApply(1, 0, V, 1, V, -m);
for (int j = 0; j < 5; j++) {
get(j);
// std::cout << -1 << " " << j << " " << get(j) << "\n";
}
for (int i = 0; i < n; i++) {
if (b[i] > 0) {
i64 cur = get(a[i]) + b[i];
int t = findFirst1(1, 0, V, cur);
i64 c = get(t - 1);
rangeApply(1, 0, V, t, t + 1, get(a[i]) + b[i] - c);
rangeApply(1, 0, V, t + 1, a[i] + 1, 0);
} else {
i64 cur = get(a[i] - 1) + 1LL * a[i] * m - b[i];
int t = findFirst2(1, 0, V, cur, m);
// std::cout << cur << " " << t << "\n";
i64 d = get(t) + b[i];
rangeApply(1, 0, V, a[i], t == -1 ? V : t, -m);
if (t != -1) {
d -= get(t - 1);
rangeApply(1, 0, V, t, t + 1, d);
}
}
// for (int j = 0; j < 5; j++) {
// std::cout << i << " " << j << " " << get(j) << "\n";
// }
}
i64 ans = get(0);
std::cout << ans << "\n";
for (int i = 0; i < cnt; i++) {
L[i] = R[i] = sum[i] = be[i] = tag[i] = 0;
}
cnt = 2;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}#include <bits/stdc++.h>
using i64 = long long;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F pred) {
return findLast(1, 0, n, l, r, pred);
}
};
constexpr i64 inf = 4E18;
struct Tag {
i64 tag = 0, bec = 0;
i64 add = 0;
void apply(const Tag &t) {
if (t.tag) {
*this = t;
} else if (tag) {
bec += t.add;
} else {
add += t.add;
}
}
};
struct Info {
i64 x = inf;
void apply(const Tag &t) {
if (t.tag) {
x = t.bec;
} else {
x += t.add;
}
}
};
Info operator+(const Info &a, const Info &b) {
return Info{std::min(a.x, b.x)};
}
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<int> v {0};
std::vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i] >> b[i];
v.push_back(a[i]);
v.push_back(a[i] - 1);
}
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
for (int i = 0; i < n; i++) {
a[i] = std::lower_bound(v.begin(), v.end(), a[i]) - v.begin();
}
int K = v.size();
// std::vector<i64> f(K, -inf);
// f[0] = 0;
// for (int i = 0; i < n; i++) {
// auto g = f;
// for (int j = 1; j < K; j++) {
// g[j] = std::max(g[j], g[j - 1] - 1LL * (v[j] - v[j - 1]) * m);
// }
// for (int j = a[i]; j < K; j++) {
// g[j] += b[i];
// }
// for (int j = K - 2; j >= 0; j--) {
// g[j] = std::max(g[j], g[j + 1]);
// }
// f = std::move(g);
// }
LazySegmentTree<Info, Tag> seg(K);
for (int i = 0; i < K; i++) {
seg.modify(i, Info{-1LL * v[i] * m});
}
for (int i = 0; i < n; i++) {
i64 p = seg.rangeQuery(a[i], a[i] + 1).x;
i64 q = seg.rangeQuery(a[i] - 1, a[i]).x;
if (b[i] > 0) {
int t = seg.findFirst(0, n,
[&](const Info &info) {
return info.x <= p + b[i];
});
seg.rangeApply(t, a[i], Tag{1, p + b[i], 0});
seg.rangeApply(a[i], K, Tag{0, 0, b[i]});
} else {
i64 sub = -b[i];
sub = std::min(sub, p - q + 1LL * (v[i] - v[i - 1]) * m);
seg.rangeApply(a[i], K, Tag{0, 0, -sub});
}
}
i64 ans = seg.rangeQuery(0, 1).x;
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}