import sys
input = sys.stdin.readline
import bisect
for test in range(int(input())):
    n, s, h = map(int, input().split())
    mh = -10000000

    inv = 0
    if s > h:
        inv = 1
        s = -s
        h = -h

    kspol = [[s, s]]
    ksotr = [[-s, s]]
    for i in range(n):
        ki, li, = map(int, input().split())
        if inv == 1:
            ki = -ki
            li = -li
        if s < li < ki and li < h:
            for j in range(i + 1, n):
                a, b = map(int, input().split())
            print(-1)
            break
        if ki < li < s:
            mh = max(mh, li)
        if ki < s < li:
            kspol.append([li, ki])
        if li < s < ki:
            ksotr.append([-li, ki])
    else:
        kspol.sort()
        mil = s
        for i in range(len(kspol)):
            mil = min(mil, kspol[i][1])
            kspol[i][1] = mil
        ksotr.sort()
        mal = s
        for i in range(len(ksotr)):
            mal = max(mal, ksotr[i][1])
            ksotr[i][1] = mal
        predotr = s + 1
        predpol = s - 1
        ans = 0
        while True:
            ind = bisect.bisect_left(kspol, [h]) - 1
            #print(kspol, ind, h, ksotr)
            nh1 = kspol[ind][1]
            #print(nh1)
            ans += abs(h - nh1)
            if nh1 == s:
                print(ans)
                break
            if nh1 < mh:
                print(-1)
                break
            if nh1 == predotr:
                print(-1)
                break
            predotr = nh1
            ind = bisect.bisect_left(ksotr, [-nh1]) - 1
            #print(ksotr[ind][1])
            nh2 = ksotr[ind][1]
            ans += abs(nh1 - nh2)
            if nh2 == s:
                print(ans)
                break
            if nh2 == predpol:
                print(-1)
                break
            predpol = nh2
            h = nh2
