#include "bits/stdc++.h"
using namespace std;

#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define all(x) begin(x),end(x)
#define sz(x) int(x.size())
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<vi> vvi;
typedef long long ll;
unordered_map<int,int>ind;
vector<pair<int,int>>keys;

void upd_int(int st, int &left, int &right, int lock) {
    if(lock>right || lock<left)return;
    if(st<=lock) {
        right=lock;
    } else {
        left=lock;
    }
}
bool checkbad(int st, int key, int lock) {
    key-=st;
    lock-=st;
    if(key<0) {
        key*=-1;
        lock*=-1;
    }
    if(lock<0)return false;
    return (key>lock);

}
void normalise(set<pair<int,int>>&s, int &left, int &right, int st) {
    while(s.size()>0 && ((*s.begin()).first<left || (*s.rbegin()).first>right)) {
        while(s.size()>0 && (*s.begin()).first<left) {
            auto p = *s.begin();
            s.erase(p);
            if(p.second==1) {
                int curind=ind[p.first];
                int other=keys[curind].second;
                s.erase({other, 2});
                upd_int(st, left, right, other);
                //right=min(right, other);
            } else {
                int curind=ind[p.first];
                int other=keys[curind].first;
                s.erase({other, 1});
            }

        }
        while(s.size()>0 && (*s.rbegin()).first>right) {
            auto p = *s.rbegin();
            s.erase(p);

            if(p.second==1) {
                int curind=ind[p.first];
                int other=keys[curind].second;
                s.erase({other, 2});
                upd_int(st, left, right, other);
                // left=max(left, other);
            } else {
                int curind=ind[p.first];
                int other=keys[curind].first;
                s.erase({other, 1});
            }
        }
    }
    
}
void solve() {
    ind.clear();
    keys.clear();
    int n;cin>>n;
    int st,h;cin>>st>>h;
    set<pair<int,int>>s;
    for(int i=0;i<n;i++) {
        int x,y;cin>>x>>y;
        ind[x]=i;
        ind[y]=i;
        s.insert({x, 1});
        s.insert({y, 2});
        keys.push_back({x,y});
    }
    int right=1e7, left=-1e7; // MAKE SURE YOU INCLUDE H
    upd_int(st,left,right,h);
    for(int i=0;i<n;i++) {
        if(checkbad(st, keys[i].first, keys[i].second)) {
            s.erase({keys[i].first, 1});
            s.erase({keys[i].second, 2});
            upd_int(st, left, right, keys[i].second);
            // if(keys[i].second>st)right=min(right, keys[i].second);
            // else left=max(left, keys[i].second);
        }
        if(st<keys[i].first and keys[i].first < keys[i].second) {
            s.erase({keys[i].first, 1});
            s.erase({keys[i].second, 2});
        }
        if(st>keys[i].first and keys[i].first > keys[i].second) {
            s.erase({keys[i].first, 1});
            s.erase({keys[i].second, 2});
        }
    }
    long long ans=0;
    while(s.size()>0) {
        upd_int(st,left,right,h);

        if(!(left<=h && h<=right)) {
            cout<<-1<<'\n';
            return;
        }
        normalise(s, left, right, st);
        if(!(left<=h && h<=right)) {
            cout<<-1<<'\n';
            return;
        }
        if(s.size()==0)break;
        if(h>st) {
            auto p = *s.begin();
            s.erase(p);
            if(p.second==2) {
                int curind=ind[p.first];
                int other=keys[curind].first;
                s.erase({other, 1});
            } else {
                ans+=abs(h-p.first);
                h=p.first;
                int curind=ind[p.first];
                int other=keys[curind].second;
                s.erase({other, 2});
                upd_int(st, left, right, other);
                // right=min(right, other);
            }
        } else {
            auto p = *s.rbegin();
            s.erase(p);
            if(p.second==2) {
                int curind=ind[p.first];
                int other=keys[curind].first;
                s.erase({other, 1});
            } else {
                ans+=abs(h-p.first);
                h=p.first;
                int curind=ind[p.first];
                int other=keys[curind].second;
                s.erase({other, 2});
                upd_int(st, left, right, other);
                // left=max(left, other);
            }
        }
    }
    ans+=abs(st-h);
    if(!(left<=h && h<=right)) {
        cout<<-1<<'\n';
        return;
    }
    cout<<ans<<'\n';
}
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;cin>>t;
    while(t--)solve();
}
/*
1
2 3 6
1 4
5 2
7
*/