#include <bits/stdc++.h>
#pragma GCC optimize(2)
// using namespace std;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
struct node
{
int x, y;
friend bool operator < (node a, node b){
return a.y < b.y;
}
};
void solve() {
int n, f;
std::cin >> n >> f;
std::vector<std::pair<int, int>> a(n);
for(auto &[x, y] : a){
std::cin >> x >> y;
}
i64 ans = 0;
std::sort(a.begin(), a.end());
std::priority_queue<node> q;
int p = 0;
while(p < n && a[p].first <= f){
q.push({a[p].first, a[p].second});
p++;
}
while(p < n){
if (q.size() && q.top().y >= f){
node cur = q.top();
q.pop();
ans += cur.y - cur.x;
f = cur.y;
while(p < n && a[p].first <= f){
q.push({a[p].first, a[p].second});
p++;
}
}else{
ans += a[p].second - cur;
cur = a[p].second;
p++;
while(p < n && a[p].first <= f){
q.push({a[p].first, a[p].second});
p++;
}
}
}
while(q.size()){
node cur = q.top();
q.pop();
ans += cur.y - cur.x;
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cout << std::fixed << std::setprecision(10); // 固定输出精度
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}