You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.

You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.

You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.

Input

The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.

Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.

It is guaranteed that the total number of monsters in all test cases does not exceed 300000.

Output

For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.

Example

Input


1
3
7 15
2 14
5 3


Output


6
def main():
    import sys
    input = sys.stdin.buffer.readline
    mod=998244353
    def GCD(x, y):
        while (y):
            x, y = y, x % y
        return x
    def lcm(a,b):
        return (a*b)//GCD(a,b)
    # Modified merge for inversion
    def merge(a,start,mid,end,d):
        p=start
        q=mid+1
        arr=[0]*(end-start+1)
        k=0
        coun=0
        for i in range(start,end+1):
            if p > mid:
                arr[k]=a[q]
                q+=1
                k+=1
            elif q > end:
                arr[k]=a[p]
                d[a[p]] += coun
                k+=1
                p+=1
            elif a[p] < a[q]:
                arr[k]=a[p]
                d[a[p]]+=coun
                k+=1
                p+=1
            else:
                arr[k]=a[q]
                k+=1
                q+=1
                coun+=1

        for i in range(k):
            a[start]=arr[i]
            start+=1
    def mergesort(a, start, end,d):
        if start<end:
            mid=(start+end)//2
            mergesort(a,start,mid,d)
            mergesort(a,mid+1,end,d)
            merge(a,start,mid,end,d)
    # inversion return dic for every index inversion count
    def inversion(l):
        d={}
        for i in l:
            d[i]=0
        mergesort(l,0,len(l)-1,d)
        return d
    def binary_search_lower(A,low,high,k):
        if high-low!=1:
            mid=(low+high)//2
            x = A[mid]
            if x>=k:
                return binary_search_lower(A,low,mid,k)
            else:
                return binary_search_lower(A,mid,high,k)
        if A[low]>=k:
            return low
        else:
            return high
    # Function to find power
    def binary_expo(x,n):
        if n==0:
            return 1
        elif n%2==0:
            return binary_expo(x*x,n//2)
        else:
            return x*binary_expo(x*x,(n-1)//2)
    # Function to find power with given modulus
    def modu_expo(x,n,m):
        if n==0:
            return 1
        elif n%2==0:
            return modu_expo((x*x)%m,n//2,m)
        else:
            return (x*modu_expo((x*x)%m,(n-1)//2,m))%m
    # Function for modular multiplicative inverse via using extended_GCD_Euclid's
    def eEuclid(c, m):
        if m == 0:
            global d
            global x
            global y
            d = c
            x = 1
            y = 0
        else:
            eEuclid(m, c % m)
            temp = x
            x = y
            y = temp - (c // m) * y
    def mod_Inv(c, m):
        eEuclid(c, m)
        return (x % m + m) % m
    # Function for nCr%p using fermat (p is prime)
    def ncr(n, r, p):
        num = den = 1
        for i in range(r):
            num = (num * (n - i)) % p
            den = (den * (i + 1)) % p
        return (num * pow(den,
                          p - 2, p)) % p
    #*************************************************************
    for _ in range(int(input())):
        n=int(input())
        l=[]
        for i in range(n):
            a,b=map(int,input().split())
            l.append([a,b])
        ans=0
        for i in range(n-1):
            if l[i][1]-l[i+1][0]<0:
                ans+=l[i+1][0]-l[i][1]
                l[i+1][0]=l[i+1][0]-(l[i+1][0]-l[i][1])
                #print(l[i+1][0],l[i][1])
        if l[-1][1]-l[0][0]<0:
            ans+=l[0][0]-l[-1][1]
            l[0][0]=l[0][0]-(l[0][0]-l[-1][1])
        min=99999999999999
        for i in range(n):
            if l[i][0]<min:
                min=l[i][0]
        print(ans+min)

if __name__ == "__main__":
    main()
