#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <cassert>
#include <random>
#include <map>
#include <queue>


#define int long long
using namespace std;
mt19937 rnd(179);
int n, s, h;
vector<pair<int, int>> a;

void inp() {
    cin >> n >> s >> h;
    a.assign(n, make_pair(-1, -1));
    for (auto& i : a) cin >> i.first >> i.second;
}

int solve() {
    for (auto& i : a) {
        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) continue;
        else if (a[i].second > s) {
            if (a[i].first > a[i].second) {
                return -1;
            } else if (a[i].first > s) continue;
            else {
                ss.insert(make_pair(a[i].second, i));
            }
        } else {
            if (a[i].second < a[i].first && a[i].first < s) continue;
            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) {
        return h - s;
    }
    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()) {
            return -1;
        }
        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;
            }
            assert(lft[ind] < ind2->first);
            ans += abs(lft[ind] - ind2->first);
            ps.emplace_back(ind2 - l2.begin(), 2);
        } else {
            int to = rgt[ind];
            if (temp[ind] < l2[ind].first) {
                return -1;
            }
            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()) {
                assert(rgt[ind] > s);
                ans += abs(s - rgt[ind]);
                break;
            }
            assert(rgt[ind] > l1[ind2 - l1.begin() - 1].first);
            ans += rgt[ind] - l1[ind2 - l1.begin() - 1].first;
            ps.emplace_back(ind2 - l1.begin() - 1, 1);
        }
        // cout << "ANS " << ans << endl;
    }
    if (ps.size() > 1) {
        int i2 = ps[1].first;
        for (int ii = i2; ii < n2; ii++) {
            if (a[l2[ii].second].first < l2[ii].first) {
                return -1;
            }
        }
    }
    // cout << "PS: ";
    // for (auto [x, y] : ps) cout << x << " " << y << "   ";
    // cout << endl;
    return ans;
    cout << ans << endl;
}

int stupidSolve() {
    set<pair<int, int>> sss;
    map<pair<set<pair<int, int>>, vector<bool>>, int> mapa;
    sss.insert(make_pair(s, 0));
    sss.insert(make_pair(h, 0));
    for (int i = 0; i < n; i++) {
        sss.insert(make_pair(a[i].first, i + 1));
        sss.insert(make_pair(a[i].second, i + 1));
    }
    mapa[make_pair(sss, vector<bool> (n, false))] = 0;
    queue<pair<set<pair<int, int>>, vector<bool>>> q;
    q.emplace(sss, vector<bool> (n, false));
    int ans = -1;
    while (!q.empty()) {
        auto [ss, us] = q.front();
        q.pop();
        auto d = mapa[make_pair(ss, us)];

        cout << "STATE: ";
        for (auto el : ss) cout << el.first << " " << el.second << "   ";
        cout << "US: ";
        for (int x : us) cout << x << " ";
        cout << "  D = " << d << endl;

        if (ans != -1 && d > ans) continue;
        int pos = -1;
        for (auto el : ss) if (el.second == 0 && el.first != h) pos = el.first;
        auto it = ss.lower_bound(make_pair(pos, 0));
        if (it != ss.begin()) {
            it--;
            auto pr = it;
            it++;
            // cout << "PR: " << pr->first << " " << pr->second << "  " << it->first << " " << it->second << endl;;
            if (pr->second == 0) {
                // cout << "A1 " << endl;
                if (ans == -1 || ans > d + abs(pos - pr->first)) {
                    ans = d + abs(pos - pr->first);
                }
            } else {
                if (a[pr->second - 1].first == pr->first) {
                    auto nss = ss;
                    int nd = d + abs(pos - pr->first);
                    auto nus = us;
                    nus[pr->second - 1] = true;
                    nss.erase(make_pair(pr->first, pr->second));
                    nss.erase(make_pair(pos, 0));
                    nss.insert(make_pair(pr->first, 0));
                    auto nel = make_pair(nss, nus);
                    if (mapa.find(nel) == mapa.end() || mapa[nel] > nd) {
                        mapa[nel] = nd;
                        q.push(nel);
                    }
                } else if (us[pr->second - 1]) {
                    auto nss = ss;
                    int nd = d + abs(pos - pr->first);
                    auto nus = us;
                    nss.erase(make_pair(pr->first, pr->second));
                    nss.erase(make_pair(pos, 0));
                    nss.insert(make_pair(pr->first, 0));
                    auto nel = make_pair(nss, nus);
                    if (mapa.find(nel) == mapa.end() || mapa[nel] > nd) {
                        mapa[nel] = nd;
                        q.push(nel);
                    }
                }
            }
        }

        if (it != --ss.end()) {
            it++;
            auto nx = it;
            it--;
            // cout << "NX : " << nx->first << " " << nx->second << endl;
            if (nx->second == 0) {
                // cout << "A2 " << endl;
                if (ans == -1 || ans > d + abs(pos - nx->first)) {
                    ans = d + abs(pos - nx->first);
                }
            } else {
                if (a[nx->second - 1].first == nx->first) {
                    auto nss = ss;
                    int nd = d + abs(pos - nx->first);
                    auto nus = us;
                    nus[nx->second - 1] = true;
                    nss.erase(make_pair(nx->first, nx->second));
                    nss.erase(make_pair(pos, 0));
                    nss.insert(make_pair(nx->first, 0));
                    auto nel = make_pair(nss, nus);
                    if (mapa.find(nel) == mapa.end() || mapa[nel] > nd) {
                        mapa[nel] = nd;
                        q.push(nel);
                    }
                } else if (us[nx->second - 1]) {
                    auto nss = ss;
                    int nd = d + abs(pos - nx->first);
                    auto nus = us;
                    nss.erase(make_pair(nx->first, nx->second));
                    nss.erase(make_pair(pos, 0));
                    nss.insert(make_pair(nx->first, 0));
                    auto nel = make_pair(nss, nus);
                    if (mapa.find(nel) == mapa.end() || mapa[nel] > nd) {
                        mapa[nel] = nd;
                        q.push(nel);
                    }
                }
            }
        }
    }

    return ans;
}

void gen() {
    n = 1 + rnd() % 5;
    vector<int> cords;
    for (int x = -10; x <= 10; x++) cords.push_back(x);
    int i = rnd() % (int)cords.size();
    s = cords[i];
    swap(cords[i], cords.back());
    cords.pop_back();
    i = rnd() % (int)cords.size();
    h = cords[i];
    swap(cords[i], cords.back());
    cords.pop_back();
    a.assign(n, make_pair(1, 1));
    for (int j = 0; j < n; j++) {
        i = rnd() % (int)cords.size();
        a[j].first = cords[i];
        swap(cords[i], cords.back());
        cords.pop_back();
        i = rnd() % (int)cords.size();
        a[j].second = cords[i];
        swap(cords[i], cords.back());
        cords.pop_back();
    }
}

void stress() {
    int it = 1;
    while (true) {
        cout << "TEST " << it << endl;
        gen();
        cout << n << " " << s << " " << h << endl;
        for (int i = 0; i < n; i++) cout << a[i].first << " " << a[i].second << endl;
        cout << endl;
        auto ans2 = stupidSolve();
        auto ans1 = solve();

        if (ans1 != ans2) {
            cout <<" FOUND " << endl;
            cout << ans1 << " " << ans2 << endl;
            return;
        }
        it++;
    }
}

signed main() {
    // stress();

    // return 0;
    // inp();
    // auto ans = stupidSolve();
    // cout << ans << '\n';
    // return 0;

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    cin >> t;
    while (t--) {
        inp();
        auto ans = solve();
        cout << ans << '\n';
    }
}