#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <cassert>

#define int long long
using namespace std;

void solve() {
    int n, s, h;
    cin >> n >> s >> h;
    vector<bool> us(n, true);
    vector<pair<int, int>> a(n);
    for (auto& i : a) {
        cin >> i.first >> i.second;
        if (s > h) {
            i.first *= -1;
            i.second *= -1;
        }
    }
    if (s > h) {
        s *= -1;
        h *= -1;
    }
    set<pair<int, int>> ss;
    ss.insert(make_pair(s, 0));
    ss.insert(make_pair(h, 0));
    for (int i = 0; i < n; i++) {
        if (a[i].second > h) us[i] = false;
        else if (a[i].second > s) {
            if (a[i].first > a[i].second) {
                cout << -1 << '\n';
                return;
            } else if (a[i].first > s) us[i] = false;
            else {
                ss.insert(make_pair(a[i].second, i));
            }
        } else {
            if (a[i].second < a[i].first && a[i].first < s) us[i] = false;
            else ss.insert(make_pair(a[i].second, i));
        }
    }
    vector<pair<int, int>> l1, l2;
    auto it = ss.lower_bound(make_pair(s, 0));
    it++;
    while (it->first != h) {
        l1.push_back(*it);
        it++;
    }
    it = ss.lower_bound(make_pair(s, 0));
    while (it != ss.begin()) {
        it--;
        l2.push_back(*it);
    }
    reverse(l2.begin(), l2.end());
    int n1 = (int)l1.size(), n2 = (int)l2.size();
    if (n1 == 0) {
        cout << h - s << '\n';
        return;
    }
    vector<int> lft(n1, 0);
    lft[0] = a[l1[0].second].first;
    for (int i = 1; i < n1; i++) {
        lft[i] = min(lft[i - 1], a[l1[i].second].first);
    }
    vector<int> rgt(n2, 0);
    vector<int> temp(n2, 0);
    if (n2) {
        temp[n2 - 1] = a[l2[n2 - 1].second].first;
        rgt[n2 - 1] = a[l2[n2 - 1].second].first;
        for (int i = n2 - 2; i >= 0; i--) {
            rgt[i] = max(rgt[i + 1], a[l2[i].second].first);
            temp[i] = min(temp[i + 1], a[l2[i].second].first);
        }
    }
    // cout << "L1: ";
    // for (auto [x, i] : l1) cout << x << " " << i << "   ";
    // cout << endl;
    // cout << "L2: ";
    // for (auto [x, i] : l2) cout << x << " " << i << "   ";
    // cout << endl;
    // cout << "LFT: ";
    // for (int x : lft) cout << x << " ";
    // cout << endl;
    // cout << "RGT: ";
    // for (int x : rgt) cout << x << " ";
    // cout << endl;
    // return ;
    int ans = h - l1[n1 - 1].first;
    vector<pair<int, int>> ps;
    ps.emplace_back(n1 - 1, 1);
    set<pair<int, int>> usd;
    while (true) {
        auto [ind, fl] = ps.back();
        if (usd.find(ps.back()) != usd.end()) {
            cout << -1 << '\n';
            return;
        }
        usd.insert(ps.back());
        // cout << "IND " << ind << " " << fl << endl;
        if (fl == 1) {
            int to = lft[ind];
            assert(lft[ind] < l1[ind].first);
            ans += l1[ind].first - lft[ind];
            auto ind2 = lower_bound(l2.begin(), l2.end(), make_pair(to, -1));
            if (ind2 == l2.end()) {
                ans += abs(s - lft[ind]);
                break;
            }
            ans += abs(lft[ind] - ind2->first);
            ps.emplace_back(ind2 - l2.begin(), 2);
        } else {
            int to = rgt[ind];
            if (temp[ind] < l2[ind].first) {
                cout << -1 << '\n';
                return;
            }
            assert(rgt[ind] > l2[ind].first);
            ans += rgt[ind] - l2[ind].first;
            auto ind2 = lower_bound(l1.begin(), l1.end(), make_pair(to, -1));
            if (ind2 == l1.begin()) {
                ans += abs(s - rgt[ind]);
                break;
            }
            ans += rgt[ind] - l1[ind2 - l1.begin() - 1].first;
            ps.emplace_back(ind2 - l1.begin() - 1, 1);
        }
        // cout << "ANS " << ans << endl;
    }
    // cout << "PS: ";
    // for (auto [x, y] : ps) cout << x << " " << y << "   ";
    // cout << endl;
    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}