#include <bits/stdc++.h>
#define pb push_back
#define pi pair< ll , ll >
#define VL vector< ll >
#define VP vector< pair < ll,ll > >
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MX 1000005
#define rr return 0;
#define inf 99999999999
#define M 1000000007
#define frl(i,a,b) for(ll i=a;i<b;i++)
#define rfl(i,a,b) for(ll i=a;i>=b;i--)

using namespace std;

template < class T >
T maxx(T A,T B){ return (A>B)?A:B; }
template < class T >
T minn(T A,T B ){ return (A<B)?A:B; }

typedef long long int ll;

int main() {
    fastio
	ll tt,n,p,m,k,i,j,l,a,b,c;
	ll r,g;
    cin>>tt; //	tt=1;

	while(tt--)
	{
	    cin>>n;
	    vector<pair<ll,ll> >v;
	    for(i=0;i<n;i++)
	    {
	        cin>>a>>b;
	        v.push_back(make_pair(a,b));
	    }
	    sort(v.begin(),v.end());
	    ll mn=inf,mx=0;
	    for(i=0;i<v.size();i++)
	    {
	        mn=minn(v[i].second ,mn);

	    }
	    for(i=0;i<v.size();i++)
	    {
	        if(v[i].first>mn)
	        {
	            mx=mn;
	            break;
	        }

	    }
	    if(v.size()>1)
	    mx=v[v.size()-1].first-mn;

	    cout<<mx<<"\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define pi pair< ll , ll >
#define VL vector< ll >
#define VP vector< pair < ll,ll > >
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MX 1000005
#define rr return 0;
#define inf 99999999999
#define M 1000000007
#define frl(i,a,b) for(ll i=a;i<b;i++)
#define rfl(i,a,b) for(ll i=a;i>=b;i--)

using namespace std;

template < class T >
T maxx(T A,T B){ return (A>B)?A:B; }
template < class T >
T minn(T A,T B ){ return (A<B)?A:B; }

typedef long long int ll;

int main() {
    fastio
	ll tt,n,p,m,k,i,j,l,a,b,c;
	ll r,g;
    cin>>tt; //	tt=1;

	while(tt--)
	{
	    cin>>n;
	    vector<pair<ll,ll> >v;
	    for(i=0;i<n;i++)
	    {
	        cin>>a>>b;
	        v.push_back(make_pair(a,b));
	    }
	    sort(v.begin(),v.end());
	    ll mn=inf,mx=0;
	    for(i=0;i<v.size();i++)
	    {
	        mn=minn(v[i].second ,mn);

	    }
	    for(i=0;i<v.size();i++)
	    {
	        if(v[i].first>mn)
	        {
	            mx=mn;
	            break;
	        }

	    }

	    mx=v[v.size()-1].first-mn;
	    if(mx<0)
	    mx=0;
	    cout<<mx<<"\n";
	}

	return 0;
}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int least = INT_MAX;
        int largest = 0;
        int times;
        cin >> times;
        for(int i = 0; i < times * 2; i++) {
            int sub;
            cin >> sub;
            if(i % 2 == 0) {
                if(sub > largest) largest = sub;
            } else {
                if(sub < least) least = sub;
            }
        }
        if(largest < least) cout << 0 << endl;
        else cout << largest - least << endl;
    }
}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int least = INT_MAX;
        int largest = 0;
        int times;
        cin >> times;
        if(times == 1) {
            cout << 0 << endl;
            continue;
        }
        for(int i = 0; i < times * 2; i++) {
            int sub;
            cin >> sub;
            if(i % 2 == 0) {
                if(sub > largest) largest = sub;
            } else {
                if(sub < least) least = sub;
            }
        }
        if(largest < least) cout << 0 << endl;
        else cout << largest - least << endl;
    }
}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int least = INT_MAX;
        int largest = 0;
        int times;
        cin >> times;
        if(times == 1) {
            cout << 0 << endl;
            continue;
        }
        for(int i = 0; i < times * 2; i++) {
            int sub;
            cin >> sub;
            if(i % 2 == 0) {
                if(sub > largest) largest = sub;
            } else {
                if(sub < least) least = sub;
            }
        }
        cout << largest - least << endl;
    }
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
#include<time.h>
#include <iomanip>

using    namespace __gnu_pbds;
using namespace std;

#define          ll                     long long int
#define          EPS                    10E-10
#define          ull                    unsigned long long int
#define          db                     double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          mxx                    100005
#define          vi                     vector<int>
#define          vl                     vector<ll>
#define          pb                     push_back
#define          sc                     scanf
#define          pf                     printf
#define          ff                     first
#define          ss                     second
#define          re                     return
#define          QI                     queue<int>
#define          SI                     stack<int>
#define          iseq(a,b)              (fabs(a-b)<eps)
#define          SZ(x)                  ((int) (x).size())
#define          scin(x)                scanf("%d",&(x))
#define          scin2(x,y)             scanf("%d %d",&(x),&(y))
#define          scln(x)                scanf("%lld",&(x))
#define          scln2(x,y)             scanf("%lld %lld",&(x),&(y))
#define          scch1(ch)              scanf("%s",ch)
#define          scch2(ch1,ch2)         scanf("%s %s",ch1,ch2)
#define          min3(a,b,c)            min(a,min(b,c))
#define          min4(a,b,c,d)          min(d,min(a,min(b,c)))
#define          max3(a,b,c)            max(a,max(b,c))
#define          max4(a,b,c,d)          max(d,max(a,max(b,c)))
#define          ms(a,b)                memset(a,b,sizeof(a))
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          input                  freopen("input.txt","rt", stdin)
#define          output                 freopen("output.txt","wt", stdout)
#define          PI                     3.141592653589793238462643
#define          rep( i , a , b )       for( i=a ; i<b ; i++)
#define          rev( i , a , b )       for( i=a ; i>=b ; i--)
#define          repx( i ,a,b, x)       for( i=a ; i<b ; i+=x)
#define          RUN_CASE(t,T)          for(__typeof(t) t=1;t<=T;t++)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          get_pos(c,x)           (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          CASEL(t)               printf("Case %d:\n",t)
#define          Unique(X)             (X).erase(unique(all(X)),(X).end())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);}
void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);}
//ll toint(string s){ll n=0,k=1;for(int i=s.size()-1; i>=0; i--){n += ((s[i]-'0')*k);k*=10;}return n;}
//string tostring(ll x){string s="";while(x){s += (x%10)+'0';x/=10;}reverse(s.begin(),s.end());return s;}
//cout<<*X.find_by_order(0)<<endl;
//cout<<X.order_of_key(-5)<<endl;

bool sortinrev(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
    return (a.first > b.first);
}

///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------

///-----------------------Bitmask------------------
///int Set(int N,int pos){return N=N | (1<<pos);}
///int reset(int N,int pos){return N= N & ~(1<<pos);}
///bool check(int N,int pos){return (bool)(N & (1<<pos));}
///------------------------------------------------

int main()
{
     FLASH();
#ifndef ONLINE_JUDGE
     time_t time_t1, time_t2;
     time_t1 = clock();
#endif
     //Start
     ll t;
     cin>>t;
     while(t--)
     {
         vector< pair<ll,ll> >v;
         ll n,mna=0,mxb=1e9;
         cin>>n;
         for(ll i=1;i<=n;i++)
         {
             ll a,b;
             cin>>a>>b;
             if(a>b)swap(a,b);
             mna=max(mna,a);
             mxb=min(mxb,b);
             v.pb(make_pair(a,b));
         }
         sort(all(v));
         v.resize(unique(all(v))-v.begin());
         if(v.size()==1)cout<<0<<endl;
         else cout<<abs(mna-mxb)<<endl;
     }

#ifndef ONLINE_JUDGE
	time_t2 = clock();
	SETF();
	cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n";
	UNSETF();
#endif

    return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
#include<time.h>
#include <iomanip>

using    namespace __gnu_pbds;
using namespace std;

#define          ll                     long long int
#define          EPS                    10E-10
#define          ull                    unsigned long long int
#define          db                     double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          mxx                    100005
#define          vi                     vector<int>
#define          vl                     vector<ll>
#define          pb                     push_back
#define          sc                     scanf
#define          pf                     printf
#define          ff                     first
#define          ss                     second
#define          re                     return
#define          QI                     queue<int>
#define          SI                     stack<int>
#define          iseq(a,b)              (fabs(a-b)<eps)
#define          SZ(x)                  ((int) (x).size())
#define          scin(x)                scanf("%d",&(x))
#define          scin2(x,y)             scanf("%d %d",&(x),&(y))
#define          scln(x)                scanf("%lld",&(x))
#define          scln2(x,y)             scanf("%lld %lld",&(x),&(y))
#define          scch1(ch)              scanf("%s",ch)
#define          scch2(ch1,ch2)         scanf("%s %s",ch1,ch2)
#define          min3(a,b,c)            min(a,min(b,c))
#define          min4(a,b,c,d)          min(d,min(a,min(b,c)))
#define          max3(a,b,c)            max(a,max(b,c))
#define          max4(a,b,c,d)          max(d,max(a,max(b,c)))
#define          ms(a,b)                memset(a,b,sizeof(a))
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          input                  freopen("input.txt","rt", stdin)
#define          output                 freopen("output.txt","wt", stdout)
#define          PI                     3.141592653589793238462643
#define          rep( i , a , b )       for( i=a ; i<b ; i++)
#define          rev( i , a , b )       for( i=a ; i>=b ; i--)
#define          repx( i ,a,b, x)       for( i=a ; i<b ; i+=x)
#define          RUN_CASE(t,T)          for(__typeof(t) t=1;t<=T;t++)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          get_pos(c,x)           (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          CASEL(t)               printf("Case %d:\n",t)
#define          Unique(X)             (X).erase(unique(all(X)),(X).end())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);}
void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);}
//ll toint(string s){ll n=0,k=1;for(int i=s.size()-1; i>=0; i--){n += ((s[i]-'0')*k);k*=10;}return n;}
//string tostring(ll x){string s="";while(x){s += (x%10)+'0';x/=10;}reverse(s.begin(),s.end());return s;}
//cout<<*X.find_by_order(0)<<endl;
//cout<<X.order_of_key(-5)<<endl;

bool sortinrev(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
    return (a.first > b.first);
}

///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------

///-----------------------Bitmask------------------
///int Set(int N,int pos){return N=N | (1<<pos);}
///int reset(int N,int pos){return N= N & ~(1<<pos);}
///bool check(int N,int pos){return (bool)(N & (1<<pos));}
///------------------------------------------------

int main()
{
     FLASH();
#ifndef ONLINE_JUDGE
     time_t time_t1, time_t2;
     time_t1 = clock();
#endif
     //Start
     ll t;
     cin>>t;
     while(t--)
     {
         vector< pair<ll,ll> >v;
         ll n,mna=0,mxb=1e9;
         cin>>n;
         for(ll i=1;i<=n;i++)
         {
             ll a,b;
             cin>>a>>b;
             if(a>b)swap(a,b);
             mna=max(mna,a);
             mxb=min(mxb,b);
             v.pb(make_pair(a,b));
         }
         sort(all(v));
         v.resize(unique(all(v))-v.begin());
         if(v.size()==1)cout<<0<<endl;
         else cout<<mna-mxb<<endl;
     }

#ifndef ONLINE_JUDGE
	time_t2 = clock();
	SETF();
	cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n";
	UNSETF();
#endif

    return 0;
}


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<bits/stdc++.h>
#include<time.h>
#include <iomanip>

using    namespace __gnu_pbds;
using namespace std;

#define          ll                     long long int
#define          EPS                    10E-10
#define          ull                    unsigned long long int
#define          db                     double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          mxx                    100005
#define          vi                     vector<int>
#define          vl                     vector<ll>
#define          pb                     push_back
#define          sc                     scanf
#define          pf                     printf
#define          ff                     first
#define          ss                     second
#define          re                     return
#define          QI                     queue<int>
#define          SI                     stack<int>
#define          iseq(a,b)              (fabs(a-b)<eps)
#define          SZ(x)                  ((int) (x).size())
#define          scin(x)                scanf("%d",&(x))
#define          scin2(x,y)             scanf("%d %d",&(x),&(y))
#define          scln(x)                scanf("%lld",&(x))
#define          scln2(x,y)             scanf("%lld %lld",&(x),&(y))
#define          scch1(ch)              scanf("%s",ch)
#define          scch2(ch1,ch2)         scanf("%s %s",ch1,ch2)
#define          min3(a,b,c)            min(a,min(b,c))
#define          min4(a,b,c,d)          min(d,min(a,min(b,c)))
#define          max3(a,b,c)            max(a,max(b,c))
#define          max4(a,b,c,d)          max(d,max(a,max(b,c)))
#define          ms(a,b)                memset(a,b,sizeof(a))
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          input                  freopen("input.txt","rt", stdin)
#define          output                 freopen("output.txt","wt", stdout)
#define          PI                     3.141592653589793238462643
#define          rep( i , a , b )       for( i=a ; i<b ; i++)
#define          rev( i , a , b )       for( i=a ; i>=b ; i--)
#define          repx( i ,a,b, x)       for( i=a ; i<b ; i+=x)
#define          RUN_CASE(t,T)          for(__typeof(t) t=1;t<=T;t++)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          get_pos(c,x)           (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          CASEL(t)               printf("Case %d:\n",t)
#define          Unique(X)             (X).erase(unique(all(X)),(X).end())

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);}
void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);}
//ll toint(string s){ll n=0,k=1;for(int i=s.size()-1; i>=0; i--){n += ((s[i]-'0')*k);k*=10;}return n;}
//string tostring(ll x){string s="";while(x){s += (x%10)+'0';x/=10;}reverse(s.begin(),s.end());return s;}
//cout<<*X.find_by_order(0)<<endl;
//cout<<X.order_of_key(-5)<<endl;

bool sortinrev(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
    return (a.first > b.first);
}

///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------

///-----------------------Bitmask------------------
///int Set(int N,int pos){return N=N | (1<<pos);}
///int reset(int N,int pos){return N= N & ~(1<<pos);}
///bool check(int N,int pos){return (bool)(N & (1<<pos));}
///------------------------------------------------

int main()
{
     FLASH();
#ifndef ONLINE_JUDGE
     time_t time_t1, time_t2;
     time_t1 = clock();
#endif
     //Start
     ll t;
     cin>>t;
     while(t--)
     {
         ll n,mna=0,mxb=1e9;
         cin>>n;
         for(ll i=1;i<=n;i++)
         {
             ll a,b;
             cin>>a>>b;
             if(a>b)swap(a,b);
             mna=max(mna,a);
             mxb=min(mxb,b);
         }
         cout<<max(mna-mxb,0LL)<<endl;
     }

#ifndef ONLINE_JUDGE
	time_t2 = clock();
	SETF();
	cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n";
	UNSETF();
#endif

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int t,n,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int minR=1001010,maxL=-10010;
        for(int i=0;i<n;i++)
        {
        cin>>x>>y;
        if(x>maxL)
        maxL=x;
        if(y<minR)
        minR=y;
        }
        if(maxL-minR>=0)
        cout<<maxL-minR<<endl;
        else
        cout<<0<<endl;

    }
}


#include<iostream>
using namespace std;
int main()
{
    int t,n,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        long long minR=10000000000,maxL=-100000000000;
        for(int i=0;i<n;i++)
        {
        cin>>x>>y;
        if(x>maxL)
        maxL=x;
        if(y<minR)
        minR=y;
        }
        if(maxL-minR>=0)
        cout<<maxL-minR<<endl;
        else
        cout<<0<<endl;

    }
}


#include<iostream>
using namespace std;
int main()
{
    long long t,n,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int minR=999999999999,maxL=-99999999999;
        for(int i=0;i<n;i++)
        {
        cin>>x>>y;
        if(x>maxL)
        maxL=x;
        if(y<minR)
        minR=y;
        }
        if(maxL-minR>=0)
        cout<<maxL-minR<<endl;
        else
        cout<<0<<endl;

    }
}


#include<iostream>
using namespace std;
int main()
{
    long long t,n,x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int minR=100100010,maxL=-10000010;
        for(int i=0;i<n;i++)
        {
        cin>>x>>y;
        if(x>maxL)
        maxL=x;
        if(y<minR)
        minR=y;
        }
        if(maxL-minR>=0)
        cout<<maxL-minR<<endl;
        else
        cout<<0<<endl;

    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cctype>
#include <stack>
#include <map>
#include <bitset>
#include <time.h>
using namespace std;
#define loop(i, st, en) for(int i = st; i <= en; ++i)
#define reverse(i, st, en) for(int i = st; i >= en; --i)
#define wh(t) while(t--)
#define mem(i, x) memset(i, x, sizeof(i))
#define ssc(x) scanf("%s", x)
#define dsc(x) scanf("%d", &x)
#define lldsc(x) scanf("%I64d",&x)
#define lfsc(x) scanf("%lf", &x)
#define csc(x) scanf("%c",&x)
#define to_(s,i) s[i]-'0'
typedef long long ll;
const int MAXN=1e3+10;
const ll MAXM=2e18;
const int MAX=1e9;
const int INF=0x3f3f3f3f;
const ll P=1e9+7;
const ll C=1e5;
int T;
int n;
struct node{
    int l,r;
}p[MAXN];
bool cmp(node a,node b){
    if(a.l<b.l) return true;
    return false;
}
int main(){
    int u,v;
    dsc(T);
    wh(T){
        dsc(n);
        loop(i,1,n){
            scanf("%d",&p[i].l);
            scanf("%d",&p[i].r);
        }
        sort(p+1,p+n+1,cmp);
        int l1=p[1].l;
        int r1=p[1].r;
        for(int i=2;i<=n;i++){
            if(p[i].l<=r1&&p[i].l>=l1) l1=p[i].l;
            else break;
        }
        int l2=p[n].l;
        int r2=p[n].r;
        for(int i=n-1;i>=1;i--){
            if(l2<=p[i].r&&p[i].r<=r2) r2=p[i].r;
            else break;
        }
        printf("%d\n",r2-r1);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cctype>
#include <stack>
#include <map>
#include <bitset>
#include <time.h>
using namespace std;
#define loop(i, st, en) for(int i = st; i <= en; ++i)
#define reverse(i, st, en) for(int i = st; i >= en; --i)
#define wh(t) while(t--)
#define mem(i, x) memset(i, x, sizeof(i))
#define ssc(x) scanf("%s", x)
#define dsc(x) scanf("%d", &x)
#define lldsc(x) scanf("%I64d",&x)
#define lfsc(x) scanf("%lf", &x)
#define csc(x) scanf("%c",&x)
#define to_(s,i) s[i]-'0'
typedef long long ll;
const int MAXN=1e3+10;
const ll MAXM=2e18;
const int MAX=1e9;
const int INF=0x3f3f3f3f;
const ll P=1e9+7;
const ll C=1e5;
int T;
int n;
struct node{
    int l,r;
}p[MAXN];
bool cmp(node a,node b){
    if(a.l<b.l) return true;
    return false;
}
int main(){
    int u,v;
    dsc(T);
    wh(T){
        dsc(n);
        if(n==1){
            printf("0\n");
            continue;
        }
        int l_=0,r_=INF;
        loop(i,1,n){
            scanf("%d",&p[i].l);
            scanf("%d",&p[i].r);
            l_=max(l_,p[i].l);
            r_=min(r_,p[i].r);
        }
        printf("%d\n",l_-r_);
//        sort(p+1,p+n+1,cmp);
//        int cnt_l=1;
//        for(int i=2;i<=n;i++){
//            if(p[i].l<=p[cnt_l].r&&p[i].r>=p[cnt_l].r){
//                cnt_l=i;
//            }
//        }
//        int cnt_r=n;
//        for(int i=n-1;i>=1;i--){
//            if(p[i].r>=p[cnt_r].l&&p[i].r<=p[cnt_r].r){
//                cnt_r=i;
//            }
//        }
//        printf("%d %d\n",p[cnt_r].l,p[cnt_l].r);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cctype>
#include <stack>
#include <map>
#include <bitset>
#include <time.h>
using namespace std;
#define loop(i, st, en) for(int i = st; i <= en; ++i)
#define reverse(i, st, en) for(int i = st; i >= en; --i)
#define wh(t) while(t--)
#define mem(i, x) memset(i, x, sizeof(i))
#define ssc(x) scanf("%s", x)
#define dsc(x) scanf("%d", &x)
#define lldsc(x) scanf("%I64d",&x)
#define lfsc(x) scanf("%lf", &x)
#define csc(x) scanf("%c",&x)
#define to_(s,i) s[i]-'0'
typedef long long ll;
const int MAXN=1e3+10;
const ll MAXM=2e18;
const int MAX=1e9;
const int INF=0x3f3f3f3f;
const ll P=1e9+7;
const ll C=1e5;
int T;
int n;
struct node{
    int l,r;
}p[MAXN];
bool cmp(node a,node b){
    if(a.l<b.l) return true;
    return false;
}
int main(){
    int u,v;
    dsc(T);
    wh(T){
        dsc(n);
        loop(i,1,n){
            scanf("%d",&p[i].l);
            scanf("%d",&p[i].r);
        }
        sort(p+1,p+n+1,cmp);
        int l=p[1].r,r=p[1].l;
        loop(i,2,n){
            l=min(p[i].r,l);
            r=max(p[i].l,r);
        }
        printf("%d\n",max(r-l,0));
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cctype>
#include <stack>
#include <map>
#include <bitset>
#include <time.h>
using namespace std;
#define loop(i, st, en) for(int i = st; i <= en; ++i)
#define reverse(i, st, en) for(int i = st; i >= en; --i)
#define wh(t) while(t--)
#define mem(i, x) memset(i, x, sizeof(i))
#define ssc(x) scanf("%s", x)
#define dsc(x) scanf("%d", &x)
#define lldsc(x) scanf("%I64d",&x)
#define lfsc(x) scanf("%lf", &x)
#define csc(x) scanf("%c",&x)
#define to_(s,i) s[i]-'0'
typedef long long ll;
const int MAXN=2e6+10;
const ll MAXM=2e18;
const int MAX=1e9;
const int INF=0x3f3f3f3f;
const ll P=1e9+7;
const ll C=1e5;
int T;
int n;
struct node{
    int l,r;
}p[MAXN];
bool cmp(node a,node b){
    if(a.l<b.l) return true;
    return false;
}
int main(){
    int u,v;
    dsc(T);
    wh(T){
        dsc(n);
        loop(i,1,n){
            scanf("%d",&p[i].l);
            scanf("%d",&p[i].r);
        }
        sort(p+1,p+n+1,cmp);
        int l=p[1].r,r=p[1].l;
        loop(i,2,n){
            l=min(p[i].r,l);
            r=max(p[i].l,r);
        }
        printf("%d\n",max(r-l,0));
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cctype>
#include <stack>
#include <map>
#include <bitset>
#include <time.h>
using namespace std;
#define loop(i, st, en) for(int i = st; i <= en; ++i)
#define reverse(i, st, en) for(int i = st; i >= en; --i)
#define wh(t) while(t--)
#define mem(i, x) memset(i, x, sizeof(i))
#define ssc(x) scanf("%s", x)
#define dsc(x) scanf("%d", &x)
#define lldsc(x) scanf("%I64d",&x)
#define lfsc(x) scanf("%lf", &x)
#define csc(x) scanf("%c",&x)
#define to_(s,i) s[i]-'0'
typedef long long ll;
const int MAXN=1e3+10;
const ll MAXM=2e18;
const int MAX=1e9;
const int INF=0x3f3f3f3f;
const ll P=1e9+7;
const ll C=1e5;
int T;
int n;
struct node{
    int l,r;
}p[MAXN];
bool cmp(node a,node b){
    if(a.l<b.l) return true;
    return false;
}
int main(){
    int u,v;
    dsc(T);
    wh(T){
        dsc(n);
//        int l_=0,r_=INF;
        loop(i,1,n){
            scanf("%d",&p[i].l);
            scanf("%d",&p[i].r);
        }
        sort(p+1,p+n+1,cmp);
        int cntr=p[1].r;
        int ans=0;
        for(int i=2;i<=n;i++){
            if(p[i].l>cntr){
                ans+=p[i].l-cntr;
                cntr=p[i].l;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

 int main(){

 int t;
 long long n, r, l, mxl=1e9, mnr=0;

 cin >> t ;

  for( int i = 0; i < t; i++){

    cin >> n;

    for(int j=0; j < n; j++){

        cin >> r >> l;
        if( r > mnr ) mnr = r;
        if( l < mxl ) mxl = l;

    }
    if( n == 1) cout<<0;
    else
    cout<<endl << mnr - mxl << endl;
    mxl = 1e9;
    mnr = 0;
  }

 }


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

 int main(){

 int t;
 long long n, r, l, mxl=1e9, mnr=0;

 cin >> t ;

  for( int i = 0; i < t; i++){

    cin >> n;

    for(int j=0; j < n; j++){

        cin >> r >> l;
        if( r >= mnr ) mnr = r;
        if( l <= mxl ) mxl = l;

    }

    if( n == 1){ cout << 0 << endl ;   mxl = 1e9;
    mnr = 0; continue; }

    if(mnr - mxl<=0){  cout << 0 << endl;   mxl = 1e9;
    mnr = 0; continue;}

    cout.precision(0);
    cout << fixed << mnr - mxl << endl;

    mxl = 1e9;
    mnr = 0;
  }

 }


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

 int main(){

 int t;
 long long n, r, l, mxl=1e9, mnr=0;

 cin >> t ;

  for( int i = 0; i < t; i++){

    cin >> n;

    for(int j=0; j < n; j++){

        cin >> r >> l;
        if( r > mnr ) mnr = r;
        if( l < mxl ) mxl = l;

    }
    if( n == 1){ cout<<0<<endl ; continue; }
    if(mnr - mxl<0){  cout<<0<<endl; continue;}
    else
    cout<<endl << mnr - mxl << endl;
    mxl = 1e9;
    mnr = 0;
  }

 }


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

 int main(){

 int t;
 long long n, r, l, mxl=1e9, mnr=0;

 cin >> t ;

  for( int i = 0; i < t; i++){

    cin >> n;

    for(int j=0; j < n; j++){

        cin >> r >> l;
        if( r >= mnr ) mnr = r;
        if( l <= mxl ) mxl = l;

    }

    if( n == 1){ cout << 0 << endl ; continue; }

    if(mnr - mxl<=0){  cout << 0 << endl; continue;}

    else
    {
    cout.precision(0);
    cout << fixed << mnr - mxl << endl;
    }

    mxl = 1e9;
    mnr = 0;
  }

 }


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

 int main(){

 int t;
 long long n, r, l, mxl=1e9, mnr=0;

 cin >> t ;

  for( int i = 0; i < t; i++){

    cin >> n;

    for(int j=0; j < n; j++){

        cin >> r >> l;
        if( r > mnr ) mnr = r;
        if( l < mxl ) mxl = l;

    }
    if( n == 1) cout<<0<<endl ;
    else
    cout<<endl << mnr - mxl << endl;
    mxl = 1e9;
    mnr = 0;
  }

 }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
   { ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;

   while(t--)
   {
    ll n;
    cin>>n;
    if(n==1)
    cout<<0<<endl;
    else
 {   ll a[n];
     ll b[n];
    for(ll i=0;i<n;i++)
    {

        cin>>a[i]>>b[i];

    }
    sort(a,a+n);
    sort(b,b+n);
    ll k=a[n-1]-b[0];
    if(k>=0)
    cout<<a[n-1]-b[0]<<endl;
    else
    cout<<0<<endl;
 }
   }
      return 0;

   }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
   { ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;

   while(t--)
   {
    ll n;
    cin>>n;
    if(n==1)
    cout<<0<<endl;
    else
 {   ll a[n];
     ll b[n];
    for(ll i=0;i<n;i++)
    {

        cin>>a[i]>>b[i];

    }
    sort(a,a+n);
    sort(b,b+n);
    cout<<a[n-1]-b[0]<<endl;
 }
   }
      return 0;

   }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
   { ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;

   while(t--)
   {
    ll n;
    cin>>n;
    if(n==1)
    cout<<0<<endl;
    else
 {   ll a[n];
     ll b[n];
    for(ll i=0;i<n;i++)
    {

        cin>>a[i]>>b[i];

    }
    sort(a,a+n);
    sort(b,b+n);
    ll k=a[n-1]-b[0];

    cout<<max(0,a[n-1]-b[0])<<endl;

 }
   }
      return 0;

   }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
   { ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;

   while(t--)
   {
    ll n;
    cin>>n;
    ll a[n];
     ll b[n];
    for(ll i=0;i<n;i++)
    {

        cin>>a[i]>>b[i];

    }
    sort(a,a+n);
    sort(b,b+n);
    ll k=a[n-1]-b[0];

cout<<max(k,0)<<endl;

 }

      return 0;

   }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
   { ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;

   while(t--)
   {
    ll n;
    cin>>n;
    ll a[n];
     ll b[n];
    for(ll i=0;i<n;i++)
    {

        cin>>a[i]>>b[i];

    }
    sort(a,a+n);
    sort(b,b+n);
    ll k=a[n-1]-b[0];
if(k>=0)
cout<<k<<endl;
else
cout<<0<<endl;
 }

      return 0;

   }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
   { ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;

   while(t--)
   {
    ll n;
    cin>>n;
    if(n==1)
    cout<<0<<endl;
    else
 {   ll a[n];
     ll b[n];
    for(ll i=0;i<n;i++)
    {

        cin>>a[i]>>b[i];

    }
    sort(a,a+n);
    sort(b,b+n);
    ll k=a[n-1]-b[0];

    cout<<max(0,(a[n-1]-b[0]))<<endl;

 }
   }
      return 0;

   }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
   { ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin>>t;

   while(t--)
   {
    ll n;
    cin>>n;
    if(n==1)
    cout<<0<<endl;
    else
 {   vector<pair<ll,ll>>v;
    for(ll i=0;i<n;i++)
    {
        ll a,b;
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    sort(v.begin(),v.end());
    cout<<v[n-1].first-v[0].second<<endl;
 }
   }
      return 0;

   }


//g++  5.4.0

#include <bits/stdc++.h>
using namespace std;
bool cmp(  pair< int,int> p,  pair< int,int> q)
{
    if(p.first<q.first) return true;
    else if(p.first==q.first)
    {
        if(p.second > q.second) return true;
    }

    return false;
}

int main()
{
        int t;cin>>t;
    while(t--)
    {
        int n,x,y;
        cin>>n;
        vector< pair< int,int> > vec;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            vec.push_back({x,y});
        }
        if(n==1) {cout<<"0\n";continue;}
        sort(vec.begin(),vec.end(),cmp);
        //for(auto it:vec)cout<<it.first<<", "<<it.second<<endl;

        int l=vec[0].second;int r=vec[vec.size()-1].first;

        cout<< r-l<<endl;
    }
    return 0;
}
/*
4 5
5 9
7 7

*/


//g++  5.4.0

#include <bits/stdc++.h>
using namespace std;
bool cmp(  pair< int,int> p,  pair< int,int> q)
{
    if(p.first<q.first) return true;
    else if(p.first==q.first)
    {
        if(p.second > q.second) return true;
    }

    return false;
}

int main()
{
        int t;cin>>t;
    while(t--)
    {
        int n,x,y;
        cin>>n;
        map<int,int> mymap;
        vector< pair< int,int> > vec;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            mymap[x]++;mymap[y+1]--;
            vec.push_back({x,y});
        }
        if(n==1) {cout<<"0\n";continue;}
  /*      4  5     7  8     100  101
        1  1     0  -1         -1
        1  2     2  1     1    0*/

        int prev=0,ans=0;
        for(auto it: mymap)
        {
            mymap[it.first]+=prev;
            prev=mymap[it.first];
            ans=max(ans, prev);
        }
/*        sort(vec.begin(),vec.end(),cmp);
        //for(auto it:vec)cout<<it.first<<", "<<it.second<<endl;

        int l=vec[0].second;int r=vec[vec.size()-1].first;
        if(l>r)
        {
            l=vec[0].first;
            if(r>=l && r<=vec[0].second) l=r;
        }
        */

        cout<< ans<<endl;
    }
    return 0;
}
/*
4 5
5 9
7 7

*/


//g++  5.4.0

#include <bits/stdc++.h>
using namespace std;
bool cmp(  pair< int,int> p,  pair< int,int> q)
{
    if(p.first<q.first) return true;
    else if(p.first==q.first)
    {
        if(p.second > q.second) return true;
    }

    return false;
}

int main()
{
        int t;cin>>t;
    while(t--)
    {
        int n,x,y;
        cin>>n;
        map<int,int> mymap;
        vector< pair< int,int> > vec;
        int l=INT_MAX,r=INT_MIN;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            l=min(l,y);
            r=max(r,x);
            mymap[x]++;mymap[y+1]--;
  //          vec.push_back({x,y});
        }
        if(n==1) {cout<<"0\n";continue;}
  /*      4  5     7  8     100  101
        1  1     0  -1         -1
        1  2     2  1     1    0*/
        if(r<=l) cout<<"0\n";
        else cout<<r-l<<endl;
        continue;

        int prev=0,ans=0;
        for(auto it: mymap)
        {
            mymap[it.first]+=prev;
            prev=mymap[it.first];
            ans=max(ans, prev);
        }
        cout<< ans<<endl;
    }
    return 0;
}
/*
4 5
5 9
7 7

*/


//By RKO
//--------------------------------------------------------------------------
//NEVER GIVE UP!! THE BEST IS YET TO COME!! JUST KEEP GOING
//--------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\n"
#define int long long
typedef long long ll;
typedef long double ld;
const int mod = 1000000007;
bool isPrime(ll n){for(ll i=2;i*i<=n;++i){if(n%i==0){return false;}}return true;}
ll factorial(ll n){return (n==1 || n==0) ? 1:n*factorial(n-1);}
ll power(ll x,ll y){ll res=1;x=x;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res%mod;}
ll ncr(ll n,ll r){ll res=1;if(r>n)return 0;if(r>n-r)r=n-r;for(ll i=0;i<r;i++){res*=(n-i);res/=(i+1);}return res;}
ll gcd(ll a,ll b){if (a==0)return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return (a/gcd(a,b)*b);}
ll max(ll a,ll b){ll ans=a>b?a:b;return ans;}
ll min(ll a,ll b){ll ans=a<b?a:b;return ans;}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t=1; cin>>t;
	while(t--)
	{
	    // freopen("input.txt", "r", stdin);
    	// freopen("output.txt", "w", stdout);
		int n; cin>>n;
		if(n==1) {cout<<0<<endl; continue;}
		//vector<pair<int,int> >v;
		int mini=INT_MAX,maxi=INT_MIN;
		int x,y;
		while(n--)
		{
			cin>>x>>y;
			mini=min(mini,y);
			maxi=max(maxi,x);
		}
		cout<<max(0,maxi-mini)<<endl;
	}
	return 0;
}


//By RKO
//--------------------------------------------------------------------------
//NEVER GIVE UP!! THE BEST IS YET TO COME!! JUST KEEP GOING
//--------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\n"
#define int long long
typedef long long ll;
typedef long double ld;
const int mod = 1000000007;
bool isPrime(ll n){for(ll i=2;i*i<=n;++i){if(n%i==0){return false;}}return true;}
ll factorial(ll n){return (n==1 || n==0) ? 1:n*factorial(n-1);}
ll power(ll x,ll y){ll res=1;x=x;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res%mod;}
ll ncr(ll n,ll r){ll res=1;if(r>n)return 0;if(r>n-r)r=n-r;for(ll i=0;i<r;i++){res*=(n-i);res/=(i+1);}return res;}
ll gcd(ll a,ll b){if (a==0)return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return (a/gcd(a,b)*b);}
ll max(ll a,ll b){ll ans=a>b?a:b;return ans;}
ll min(ll a,ll b){ll ans=a<b?a:b;return ans;}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t=1; cin>>t;
	while(t--)
	{
	    // freopen("input.txt", "r", stdin);
    	// freopen("output.txt", "w", stdout);
		int n; cin>>n;
		//if(n==1) {cout<<0<<endl; continue;}
		//vector<pair<int,int> >v;
		int mini=INT_MAX,maxi=INT_MIN;

		while(n--)
		{
			int x,y;
			cin>>x>>y;
			mini=min(mini,y);
			maxi=max(maxi,x);
		}
		cout<<max(0,maxi-mini)<<endl;
	}
	return 0;
}


//By RKO
//--------------------------------------------------------------------------
//NEVER GIVE UP!! THE BEST IS YET TO COME!! JUST KEEP GOING
//--------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\n"
#define int long long
typedef long long ll;
typedef long double ld;
const int mod = 1000000007;
bool isPrime(ll n){for(ll i=2;i*i<=n;++i){if(n%i==0){return false;}}return true;}
ll factorial(ll n){return (n==1 || n==0) ? 1:n*factorial(n-1);}
ll power(ll x,ll y){ll res=1;x=x;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res%mod;}
ll ncr(ll n,ll r){ll res=1;if(r>n)return 0;if(r>n-r)r=n-r;for(ll i=0;i<r;i++){res*=(n-i);res/=(i+1);}return res;}
ll gcd(ll a,ll b){if (a==0)return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return (a/gcd(a,b)*b);}
ll max(ll a,ll b){ll ans=a>b?a:b;return ans;}
ll min(ll a,ll b){ll ans=a<b?a:b;return ans;}

signed main()
{
	int t=1; cin>>t;
	while(t--)
	{
	    // freopen("input.txt", "r", stdin);
    	// freopen("output.txt", "w", stdout);
		int n; cin>>n;
		if(n==1) {cout<<0<<endl; continue;}
		//vector<pair<int,int> >v;
		int mini=INT_MAX,maxi=INT_MIN;
		int x,y;
		while(n--)
		{
			cin>>x>>y;
			mini=min(mini,y);
			maxi=max(maxi,x);
		}
		cout<<abs(maxi-mini)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define int           long long
#define pb            push_back
#define ppb           pop_back
#define si            set <int>
#define endl          '\n'
#define fr            first
#define sc            second
#define mii           map<int ,int >
#define msi           map<string,int >
#define mis           map<int , string>
#define mci             map<char,int>
#define rep(i,a,b)    for(int  i=a;i<b;i++)
#define repr(i,a,b)      for(int i=b-1;i>=a;i--)
#define trav(it,v)      for(auto it=v.begin();it!=v.end();it++)
#define all(v)        v.begin(),v.end()
#define srt(v)        sort(all(v))
#define pii           pair<int ,int >
#define vi            vector<int >
#define vii           vector<pair<int , int >>
#define vs            vector<string>
#define sz(x)         (int )x.size()
#define M             1000000007
#define bs            binary_search
#define mp            make_pair
#define sp(n)         setprecision(n)
#define spl           " "
#define arr(a,n)      rep(i,0,n) cin>>a[i]
#define mod           998244353
#define MAXN             1000000

using namespace std;

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}

void solve()
{
	int n;
	cin>>n;
	int a,b;
	vector<pair<int,int>>v;
	rep(i,0,n)
	{
		cin>>a>>b;
		v.pb({a,b});
	}
	sort(all(v));
	int ans,c=v[0].sc,d;
	if(n==1)
	{
		cout<<"0"<<"\n";
		return;
	}
	rep(i,0,n-1)
	{
		if(v[i].sc<c)
			c=v[i].sc;
	}
	ans=v[n-1].fr-c;
	cout<<ans<<"\n";
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define int           long long
#define pb            push_back
#define ppb           pop_back
#define si            set <int>
#define endl          '\n'
#define fr            first
#define sc            second
#define mii           map<int ,int >
#define msi           map<string,int >
#define mis           map<int , string>
#define mci             map<char,int>
#define rep(i,a,b)    for(int  i=a;i<b;i++)
#define repr(i,a,b)      for(int i=b-1;i>=a;i--)
#define trav(it,v)      for(auto it=v.begin();it!=v.end();it++)
#define all(v)        v.begin(),v.end()
#define srt(v)        sort(all(v))
#define pii           pair<int ,int >
#define vi            vector<int >
#define vii           vector<pair<int , int >>
#define vs            vector<string>
#define sz(x)         (int )x.size()
#define M             1000000007
#define bs            binary_search
#define mp            make_pair
#define sp(n)         setprecision(n)
#define spl           " "
#define arr(a,n)      rep(i,0,n) cin>>a[i]
#define mod           998244353
#define MAXN             1000000

using namespace std;

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}

void solve()
{
	int n;
	cin>>n;
	int a,b;
	vector<pair<int,int>>v;
	rep(i,0,n)
	{
		cin>>a>>b;
		v.pb({a,b});
	}
	sort(all(v));
	int ans,c=v[0].sc,d;
	if(n==1)
	{
		cout<<"0"<<"\n";
		return;
	}
	rep(i,0,n-1)
	{
		if(v[i].sc<c)
			c=v[i].sc;
	}
	if(c>v[n-1].fr)
		ans=0;
	else
	ans=v[n-1].fr-c;
	cout<<ans<<"\n";
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define int           long long
#define pb            push_back
#define ppb           pop_back
#define si            set <int>
#define endl          '\n'
#define fr            first
#define sc            second
#define mii           map<int ,int >
#define msi           map<string,int >
#define mis           map<int , string>
#define mci             map<char,int>
#define rep(i,a,b)    for(int  i=a;i<b;i++)
#define repr(i,a,b)      for(int i=b-1;i>=a;i--)
#define trav(it,v)      for(auto it=v.begin();it!=v.end();it++)
#define all(v)        v.begin(),v.end()
#define srt(v)        sort(all(v))
#define pii           pair<int ,int >
#define vi            vector<int >
#define vii           vector<pair<int , int >>
#define vs            vector<string>
#define sz(x)         (int )x.size()
#define M             1000000007
#define bs            binary_search
#define mp            make_pair
#define sp(n)         setprecision(n)
#define spl           " "
#define arr(a,n)      rep(i,0,n) cin>>a[i]
#define mod           998244353
#define MAXN             1000000

using namespace std;

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
       return (a.first > b.first);
}

void solve()
{
	int n;
	cin>>n;
	int a,b;
	vector<pair<int,int>>v;
	rep(i,0,n)
	{
		cin>>a>>b;
		v.pb({a,b});
	}
	sort(all(v));
	int ans;
	if(v.size()==1)
		ans=0;
	else
	ans=v[n-1].fr-v[0].sc;
	cout<<ans<<"\n";
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ii = pair<int,int>;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<ii> v(n);
        for (auto &x:v)
            cin >> x.first >> x.second;
        sort(v.begin(), v.end());
        cout << max(v[n - 1].first - v[0].second, 0) << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
using ii = pair<int,int>;

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int L, R;
        int ansl = INT_MIN, ansr = INT_MAX;
        for (int i = 0; i < n; i++) {
            cin >> L >> R;
            ansl = max(L, ansl);
            ansr = min(ansr, R);
        }
        cout << max(ansl - ansr, 0) << "\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
       ll l[n],r[n];
       for(int i=0;i<n;i++)
       {
           cin>>l[i]>>r[i];
       }
       if(n==1)
       cout<<0<<endl;
       else
       {
           ll mini=INT_MAX;
           ll maxi=INT_MIN;
           for(int i=0;i<n;i++)
           {
               if(l[i]>maxi)
               maxi=l[i];

               if(r[i]<mini)
               mini=r[i];
           }
           cout<<maxi-mini<<endl;
       }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
       ll l[n],r[n];
       for(int i=0;i<n;i++)
       {
           cin>>l[i]>>r[i];
       }
       if(n==1)
       cout<<0<<endl;
       else
       {
           ll mini=INT_MAX;
           ll maxi=INT_MIN;
           for(int i=0;i<n;i++)
           {
               if(l[i]>maxi)
               maxi=l[i];

               if(r[i]<mini)
               mini=r[i];
           }
           cout<<max((ll)0,maxi-mini)<<endl;
       }

    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
       int n;
       cin>>n;
       ll l[n],r[n];
       for(int i=0;i<n;i++)
       {
           cin>>l[i]>>r[i];
       }
       if(n==1)
       cout<<0<<endl;
       else
       {
           ll mini=INT_MAX;
           ll maxi=INT_MIN;
           for(int i=0;i<n;i++)
           {
               if(l[i]>maxi)
               maxi=l[i];

               if(r[i]<mini)
               mini=r[i];
           }
           cout<<maxi-mini<<endl;
       }

    }

    return 0;
}s


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

#define lli long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f(i,n) for(lli i=0;i<n;i++)
#define mod 1000000009
#define pb push_back
#define pop pop_back
#define mp make_pair
#define endl "\n"

//written by me

int main() {
    ios
    lli t;
    cin>>t;
    f(tt,t)
    {
        lli n;
        cin>>n;
        lli l[n],r[n];
        f(i,n) cin>>l[i]>>r[i];
        if(n==1)
        {
            cout<<'0'<<endl;
        }
        else
        {
            /*lli ml=0,mr=1000000010;
            f(i,n)
            {
                if(l[i]>ml) ml=l[i];
                if(r[i]<mr) mr=r[i];
            }*/
            sort(l,l+n); sort(r,r+n);
            if(l[n-1]>=r[0]) cout<<l[n-1]-r[0]<<endl;
            else cout<<'0'<<endl;
        }

    }

    return 0;
}


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

#define lli long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f(i,n) for(lli i=0;i<n;i++)
#define mod 1000000009
#define pb push_back
#define pop pop_back
#define mp make_pair
#define endl "\n"

//written by me

int main() {
    ios
    lli t;
    cin>>t;
    f(tt,t)
    {
        lli n;
        cin>>n;
        lli l[n],r[n];
        f(i,n) cin>>l[i]>>r[i];
        if(n==1)
        {
            cout<<'0'<<endl;
        }
        else
        {
            lli ml=0,mr=1000;
            f(i,n)
            {
                if(l[i]>ml) ml=l[i];
                if(r[i]<mr) mr=r[i];
            }
            cout<<abs(mr-ml)<<endl;
        }

    }

    return 0;
}


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

#define lli long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define f(i,n) for(lli i=0;i<n;i++)
#define mod 1000000009
#define pb push_back
#define pop pop_back
#define mp make_pair
#define endl "\n"

//written by me

int main() {
    ios
    lli t;
    cin>>t;
    f(tt,t)
    {
        lli n;
        cin>>n;
        lli l[n],r[n];
        f(i,n) cin>>l[i]>>r[i];
        if(n==1)
        {
            cout<<'0'<<endl;
        }
        else
        {
            lli ml=0,mr=1000;
            f(i,n)
            {
                if(l[i]>ml) ml=l[i];
                if(r[i]<mr) mr=r[i];
            }
            if(ml>=mr) cout<<ml-mr<<endl;
            else cout<<'0'<<endl;
        }

    }

    return 0;
}


#include <iostream>

using namespace std;

int main()
{ long long n,k,l0 = 99999999999,r0=99999999999,r1 = -1,l1=-1,l,r;
cin >> k;
for(int i = 0;i < k; i++){
    cin >> n;
    for(int i = 0; i <n; i++){
        cin >> l >> r;
        r0 = min(r,r0);
        l1 = max(l,l1);
    }
    if(n == 1){cout << 0<<endl;return 0;}
    cout << l1-r0<<endl;
}
    return 0;
}


#include <iostream>

using namespace std;

int main()
{ long long n,k,r0=99999999999,l1=-1,l,r;
cin >> k;
for(int i = 0;i < k; i++){
    cin >> n;
    for(int i = 0; i <n; i++){
        cin >> l >> r;
        r0 = min(r,r0);
        l1 = max(l,l1);
    }
    if(n == 1 || (l1-r0) <= 0){cout << 0<<endl;}
    else{cout << l1-r0<<endl;}
    r0=99999999999;l1=-1;
}
    return 0;
}


#include <iostream>

using namespace std;

int main()
{ long long n,k,r0=99999999999,l1=-1,l,r;
cin >> k;
for(int i = 0;i < k; i++){
    cin >> n;
    for(int i = 0; i <n; i++){
        cin >> l >> r;
        r0 = min(r,r0);
        l1 = max(l,l1);
    }
    if(n == 1){cout << 0<<endl;}
    else{cout << l1-r0<<endl;}
    r0=99999999999;l1=-1;
}
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int T;cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        if(n==1){
            int a,b;cin>>a>>b;
            cout<<"0"<<endl;continue;
        }
        else{
            int mx=INT_MIN,mn=INT_MAX;
            for(int i=0;i<n;i++)
            {
                int a,b;cin>>a>>b;
                mx=max(mx,a);
                mn= min(mn,b);
            }
            int val = <mx-mn
            if(val<0)cout<<val<<endl;
            else cout<<val<<endl;
        }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int T;cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        if(n==1){
            int a,b;cin>>a>>b;
            cout<<"0"<<endl;continue;
        }
        else{
            int mx=INT_MIN,mn=INT_MAX;
            for(int i=0;i<n;i++)
            {
                int a,b;cin>>a>>b;
                mx=max(mx,a);
                mn= min(mn,b);
            }
            int val =(mx-mn);
            if(val<0){cout<<"0"<<endl;}
            else{ cout<<val<<endl;}
        }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int T;cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        if(n==1){
            int a,b;cin>>a>>b;
            cout<<"0"<<endl;continue;
        }
        else{
            int mx=INT_MIN,mn=INT_MAX;
            for(int i=0;i<n;i++)
            {
                int a,b;cin>>a>>b;
                mx=max(mx,a);
                mn= min(mn,b);
            }
            cout<<mx-mn<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int cost[2005];
int a[2005];

int main(){

      int t;
      cin >> t;

      while(t--){
         long long lmax = 0 , rmin = 1e9+1;
         int n;
         cin >> n;
         while(n--)
         {
         long long x , y;
         cin >> x >> y;
         lmax = max(lmax,x);
         rmin = min(rmin,y);

         }

         cout << max(0LL,lmax-rmin) << endl;

      }

}


#include <iostream>
#include <algorithm>
using namespace std;

int cost[2005];
int a[2005];

int main(){

      int t;
      cin >> t;
      long long lmax = 0 , rmin = 1e9+1;
      while(t--){
         int n;
         cin >> n;
         while(n--)
         {
         long long x , y;
         cin >> x >> y;
         lmax = max(lmax,x);
         rmin = min(rmin,y);

         }

         cout << max(0LL,lmax-rmin) << endl;

      }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define pi                 3.1415926535897932384626433832795
#define ll                 long long
#define pb                 push_back
#define pii                pair < int , int >
#define vecsort(v)         sort(v.begin(),v.end())
#define lcm(x,y)           (x/__gcd(x,y))*y
#define forn(i,n)          for(i = 0 ; i < n ; i++)
#define deb(x)             cout << #x << " " << x << endl;
#define vecsum(v)          accumulate(v.begin(), v.end(), 0)
#define adj_diff(a,b)      adjacent_difference(a.begin(), a.end(), b.begin())
#define IOTA(a,x)          iota(a.begin(), a.end(), x);
#define par_sum(a,sum)     partial_sum(a,a+sizeof(a),sum)
#define fast_cin           ios_base::sync_with_stdio(false);cin.tie(NULL)

ll i , j , k;

pair < int , int > p;

int main()
{
     int t;
     cin >> t;
     while(t--){
        ll n ;
        ll lmax=0,rmin=2000000000;
        cin >> n ;
        for( int i = 0 ; i < n ; i++ ){
            ll x , y;
            cin >> x >> y;
            lmax = max(lmax,x);
            rmin = min(rmin,y);
        }
        cout << max(0LL,lmax-rmin) << endl;
     }

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
	cin>>t;
	while(t--){
		int l,r,min1=100000000000000,max1=0;
		cin>>n;
		for(int i=1;i<=n;i++) {
                cin>>l>>r;
                min1=min(min1,r);
                max1=max(max1,l);
		}
		cout<<max(max1-min1,0)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
	cin>>t;
	while(t--){
		int r,l;
		int min1=5000000000;
		int max1=0;

		cin>>n;
		for(int i=1;i<=n;i++) {
                cin>>l>>r;
                min1=min(min1,r);
                max1=max(max1,l);
		}
		cout<<max(max1-min1,0)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
	cin>>t;
	while(t--){
		int l,r,min1=100000000000000;max1=0;
		cin>>n;
		for(int i=1;i<=n;i++) {
                cin>>l>>r;
                min1=min(min1,r);
                max1=max(max1,l);
		}
		cout<<max(max1-min1,0)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
	cin>>t;
	while(t--){
		int r,l;
		int min1=2000000000;
		int max1=0;

		cin>>n;
		for(int i=1;i<=n;i++) {
                cin>>l>>r;
                min1=min(min1,r);
                max1=max(max1,l);
		}
		cout<<max(max1-min1,0)<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
	cin>>t;
	while(t--){
		int l,r,min1=100000000000000;max1=0;
		cin>>n;
		for(int i=1;i<=n;i++) {
                cin>>l>>r;
                min1=min(min1,r),
                max1=max(max1,l);
		}
		cout<<max(max1-min1)<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long iint;

bool cmp(pair<iint, iint> a, pair<iint, iint> b) {
	if (a.first < b.first) {
		return 1;
	} else {
		if (a.first > b.first) {
			return 0;
		} else {
			return a.second > b.second;
		}
	}
}

int Solve() {
	int t; cin >> t;

	for (int i = -1; ++i < t;) {
		int n; cin >> n;
		vector<pair<iint, iint>> v;
		for (int j = -1; ++j < n;) {
			iint x, y; cin >> x >> y;
			v.push_back({x, y});
		}
		sort(v.begin(), v.end(), cmp);
		if (n == 1) {
			cout << 0 << '\n';
			continue;
		}
		cout << v[v.size() - 1].first - v[0].second << '\n';
	}

	return 0;
}

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

	cout.precision(22);

	return Solve();
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long iint;

bool cmp(pair<iint, iint> a, pair<iint, iint> b) {
	if (a.first < b.first) {
		return 1;
	} else {
		if (a.first > b.first) {
			return 0;
		} else {
			return a.second > b.second;
		}
	}
}

int Solve() {
	int t; cin >> t;

	for (int i = -1; ++i < t;) {
		int n; cin >> n;
		vector<pair<iint, iint>> v;
		for (int j = -1; ++j < n;) {
			iint x, y; cin >> x >> y;
			v.push_back({x, y});
		}
		sort(v.begin(), v.end(), cmp);
		cout << v[v.size() - 1].second - v[0].second << '\n';
	}

	return 0;
}

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

	cout.precision(22);

	return Solve();
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long iint;

bool cmp(pair<iint, iint> a, pair<iint, iint> b) {
	if (a.first < b.first) {
		return 1;
	} else {
		if (a.first > b.first) {
			return 0;
		} else {
			return a.second < b.second;
		}
	}
}

int Solve() {
	int t; cin >> t;

	for (int i = -1; ++i < t;) {
		int n; cin >> n;
		vector<pair<iint, iint>> v;
		for (int j = -1; ++j < n;) {
			iint x, y; cin >> x >> y;
			v.push_back({x, y});
		}
		sort(v.begin(), v.end(), cmp);
		if (n == 1) {
			cout << 0 << '\n';
			continue;
		}
		if (v[0].second > v[v.size() - 1].first) {
			cout << 0 << '\n';
		} else {
			cout << v[v.size() - 1].first - v[0].second << '\n';
		}
	}

	return 0;
}

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

	cout.precision(22);

	return Solve();
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long iint;

bool cmp(pair<iint, iint> a, pair<iint, iint> b) {
	if (a.first < b.first) {
		return 1;
	} else {
		if (a.first > b.first) {
			return 0;
		} else {
			return a.second > b.second;
		}
	}
}

int Solve() {
	int t; cin >> t;

	for (int i = -1; ++i < t;) {
		int n; cin >> n;
		vector<pair<iint, iint>> v;
		for (int j = -1; ++j < n;) {
			iint x, y; cin >> x >> y;
			v.push_back({x, y});
		}
		sort(v.begin(), v.end(), cmp);
		if (n == 1) {
			cout << 0 << '\n';
			continue;
		}
		if (v[0].second > v[v.size() - 1].first) {
			cout << 0 << '\n';
		} else {
			cout << v[v.size() - 1].first - v[0].second << '\n';
		}
	}

	return 0;
}

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

	cout.precision(22);

	return Solve();
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long iint;

int Solve() {
	int t; cin >> t;

	for (int i = -1; ++i < t;) {
		int n; cin >> n;
		vector<iint> v, m;
		for (int j = -1; ++j < n;) {
			iint x, y; cin >> x >> y;
			v.push_back(x);
			m.push_back(y);
		}
		sort(v.begin(), v.end());
		sort(m.begin(), m.end());
		if (n == 1) {
			cout << 0 << '\n';
			continue;
		}
		if (v[v.size() - 1] - m[0] < 0) {
			cout << 0 << '\n';
		} else {
			cout << v[v.size() - 1] - m[0] << '\n';
		}
	}

	return 0;
}

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

	cout.precision(22);

	return Solve();
}


#include <iostream>
#include <algorithm>

using namespace std;
const int DIM = 100001;
struct chemical
{
	int el, cst;
}d[2 * DIM];
int ind[2 * DIM];
bool cmp(chemical a, chemical b);
bool exists(int nr, int d);
bool quicksort(int nr, int s, int d);
void insert(int nr, int s, int d, int bd);
int n, m, s;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> d[i].el >> d[i].cst;
	cin >> m;
	for (int i = 1; i <= m; ++i)
		cin >> d[n + i].el >> d[n + i].cst;
	sort(d + 1, d + m + n + 1, cmp);

	int cnt = 1;
	for (int i = 1; i <= m + n; ++i)
	{
		if (cnt == 1 || !exists(d[i].el, cnt - 1))
		{
			insert(d[i].el, 1, (cnt == 1 ? cnt : cnt - 1), (cnt == 1 ? cnt : cnt - 1));
			cnt++;
			s += d[i].cst;
		}
	}

	cout << s;
	return 0;
}

bool cmp(chemical a, chemical b)
{
	return a.cst > b.cst;
}

bool exists(int nr, int d)
{
	return quicksort(nr, 1, d);
}

bool quicksort(int nr, int s, int d)
{
	int pas = 1 << 17, i = 0;
	while (pas)
	{
		if (i + pas <= d && ind[i + pas] <= nr)
			i += pas;
		pas /= 2;
	}

	return ind[i] == nr;
}

void insert(int nr, int s, int d, int bd)
{
	if (s < d)
	{
		int md = (s + d) / 2;
		if (nr > ind[md])
			insert(nr, md + 1, d, bd);
		if (nr < ind[md])
			insert(nr, s, md - 1, bd);
	}
	else
	{
		if (ind[s] > nr)
		{
			for (int i = bd + 1; i > s; --i)
			{
				ind[i] = ind[i - 1];
			}
			ind[s] = nr;
		}
		else
		{
			if(ind[1]) s++;
			for (int i = bd + 1; i > s; --i)
			{
				ind[i] = ind[i - 1];
			}
			ind[s] = nr;

		}
	}
}


#include <iostream>
#include <algorithm>

using namespace std;
const int DIM = 100001;
struct chemical
{
	int el, cst;
}d[2 * DIM];
int ind[2 * DIM];
bool cmp(chemical a, chemical b);
bool exists(int nr, int d);
bool quicksort(int nr, int s, int d);
void insert(int nr, int s, int d, int bd);
int n, m, s;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> d[i].el >> d[i].cst;
	cin >> m;
	for (int i = 1; i <= m; ++i)
		cin >> d[n + i].el >> d[n + i].cst;
	sort(d + 1, d + m + n + 1, cmp);

	int cnt = 1;
	for (int i = 1; i <= m + n; ++i)
	{
		if (cnt == 1 || !exists(d[i].el, cnt - 1))
		{
			insert(d[i].el, 1, cnt - 1, cnt - 1);
			cnt++;
			s += d[i].cst;
		}
	}

	cout << s;
	return 0;
}

bool cmp(chemical a, chemical b)
{
	return a.cst > b.cst;
}

bool exists(int nr, int d)
{
	return quicksort(nr, 1, d);
}

bool quicksort(int nr, int s, int d)
{
	int pas = 1 << 17, i = 0;
	while (pas)
	{
		if (i + pas <= d && ind[i + pas] <= nr)
			i += pas;
		pas /= 2;
	}

	return ind[i] == nr;
}

void insert(int nr, int s, int d, int bd)
{
	int pas = 1 << 17, i = 0;
	while (pas)
	{
		if (i + pas <= d && ind[i + pas] <= nr)
			i += pas;
		pas /= 2;
	}

	for (int ij = bd + 1; ij > i; --ij)
		ind[ij] = ind[ij - 1];
	ind[i + 1] = nr;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n; cin>>n;
	map<int,int> m;
	for(int i=0;i<n;i++)
	{
		int a,x; cin>>a>>x;
		m[a]=x;
	}
	int m1; cin>>m1;
	for(int i=0;i<m1;i++)
	{
		int b,y; cin>>b>>y;
		if(!m[b])
		m[b]=y;
		else if(y>m[b])
		m[b]=y;
	}
	int ans=0;
	map<int,int> :: iterator it=m.begin();
	for(;it!=m.end();it++)
	{
	    //cout<<it->first<<"<-->"<<it->second<<"\n";
		ans+=(it->second);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n; cin>>n;
	map<int,int> m;
	for(int i=0;i<n;i++)
	{
		int a,x; cin>>a>>x;
		m[a]=x;
	}
	int m1; cin>>m1;
	for(int i=0;i<m1;i++)
	{
		int b,y; cin>>b>>y;
		if(!m[b])
		m[b]=y;
		else if(y>m[b])
		m[b]=y;
	}
	int ans=0;
	map<int,int> :: iterator it=m.begin();
	for(;it!=m.end();it++)
	{
	    cout<<it->first<<"<-->"<<it->second<<"\n";
		ans+=(it->second);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n; cin>>n;
	map<long long,long long> m;
	for(long long i=0;i<n;i++)
	{
		long long a,x; cin>>a>>x;
		m[a]=x;
	}
	long long m1; cin>>m1;
	for(long long i=0;i<m1;i++)
	{
		long long b,y; cin>>b>>y;
		if(!m[b])
		m[b]=y;
		else if(y>m[b])
		m[b]=y;
	}
	long long ans=0;
	map<long long,long long> :: iterator it=m.begin();
	for(;it!=m.end();it++)
	{
	    //cout<<it->first<<"<-->"<<it->second<<"\n";
		ans+=(it->second);
	}
	cout<<ans;
}


#include <bits/stdc++.h>

using namespace std;

int n1, n2;
int a1[100005], a2[100005];
int v1[100005], v2[100005];
map<int, int> ex1;
long long vmax = 0LL;

int main()
{
	ex1.clear();

	cin >> n1;
	for (int i = 1; i <= n1; i++)
	{
		cin >> a1[i] >> v1[i];
		vmax += (long long)v1[i];
		ex1[a1[i]] = v1[i];
	}

	cin >> n2;
	for (int i = 1; i <= n2; i++)
	{
		cin >> a2[i] >> v2[i];
		vmax += (long long)v2[i];

		if (ex1[a2[i]] != 0) vmax -= (long long)min(ex1[a2[i]], v2[i]);
	}

	cout << vmax << '\n';

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long sum=0;
    int x,y,n,m;
    map<int,int>s;
   scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
         scanf("%d%d",&x,&y);
         s[x]=y;
         sum+=y;
    }
    scanf("%d",&m);
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d",&x,&y);
        if(s[x]<y)
        {
            sum-=s[x];
            sum+=y;
        }
    }
    printf("%lld\n",sum);
    return 0;
}


#include<bits/stdc++.h>
#define fol(i,a,b) for(int i=a;i<=(b);i++)
#define bor(i,a,b) for(int i=a;i>=(b);--i)
#define int long long
using namespace std;
map<int,int> mp;
signed main(){
    int n,m,tmp;
    cin>>n;
    fol(i,1,n){
        cin>>m>>tmp;
        mp[m]=max(mp[m],tmp);
    }
    cin>>n;
    fol(i,1,n){
        cin>>m>>tmp;
        mp[m]=max(mp[m],tmp);
    }
    int sum=0;
    for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
        sum+=(*it).second;
    }
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<iomanip>
#include<unordered_map>
#include<math.h>
using namespace std;

int main()
{
unordered_map <long,long> m;
long a,b;
long sum=0;
cin>>a;
for(int i=0;i<a;i++)
{
  long p,q;
  cin>>p>>q;
  m[p]=q;
  sum=sum+q;
}
cin>>b;
for(long i=0;i<b;i++)
{
  int p,q;
  cin>>p>>q;
  if(q>m[p])
  {

    sum=sum-m[p]+q;

  }

}
cout<<sum;
}


#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<iomanip>
#include<unordered_map>
#include<math.h>
using namespace std;

int main()
{
unordered_map <long,long> m;
long long a,b;
long long sum=0;
cin>>a;
for(long long i=0;i<a;i++)
{
 long long p,q;
  cin>>p>>q;
  m[p]=q;
  sum=sum+q;
}
cin>>b;
for(long long i=0;i<b;i++)
{
  long long p,q;
  cin>>p>>q;
  if(q>m[p])
  {

    sum=sum-m[p]+q;

  }

}
cout<<sum;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
bool check(const pair<int, int> &a, const pair<int, int> &b){
    if(a.second == b.second)
        return a.first > b.first;
    return a.second > b.second;
}
int main()
{
    vector< pair <int ,int> >CF;
    vector< pair <int ,int> >TC;
    ll n,m,a,x;
    ull sum1=0,sum2=0;
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a>>x;
        sum1 += x;
        CF.push_back(make_pair(a,x));
    }sort(CF.begin(), CF.end(), check);
    cin>>m;
    for(int i=0; i<m; i++){
        cin>>a>>x;
        sum2 += x;
        TC.push_back(make_pair(a,x));
    }sort(TC.begin(), TC.end(), check);
    vector< pair<int,int> > :: const_iterator i,j;
    cout<<sum1<<" "<<sum2<<endl;
    for(i=CF.end()-1,j=TC.end()-1;i>=CF.begin()&&j>=TC.begin(); i--,j--){
        int chmID = i->first;
        int chmC  = i->second;
        int TopID = j->first;
        int TopC  = j->second;
        cout<<chmC<<" "<<TopC<<endl;
        if(sum1==sum2)break;
        if(n==1&&sum1>sum2 || m==1 && sum1<sum2){cout<<sum1+sum2<<endl;return 0;}
        if(sum1>sum2){
            sum1 -= chmC;
            //if(sum1<sum2){sum1 += chmC;}
        }else{
            sum2 -= TopC;
            //if(sum2<sum1){sum2 += TopC;}
        }
        n--,m--;
    }
    cout<<sum1+sum2<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define MAX                 10000007

int main()
{
    map<ull,ull>lst;
    ull sum=0,n,m,a,x,len;
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a>>x;
        lst[a] = x;
    }
    cin>>m;
    for(int i=0; i<m; i++){
        cin>>a>>x;
        if(lst[a]<x) lst[a] = x;
    }
    map <ull, ull> :: const_iterator i;
    for(i=lst.begin(); i != lst.end(); i++){
        ull q = i->second;
        sum += q;
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
bool check(const pair<int, int> &a, const pair<int, int> &b){
    if(a.second == b.second)
        return a.first > b.first;
    return a.second > b.second;
}
int main()
{
    vector< pair <int ,int> >CF;
    vector< pair <int ,int> >TC;
    ull sum1=0,sum2=0,n,m,a,x;
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a>>x;
        sum1 += x;
        CF.push_back(make_pair(a,x));
    }sort(CF.begin(), CF.end(), check);
    cin>>m;
    for(int i=0; i<m; i++){
        cin>>a>>x;
        sum2 += x;
        TC.push_back(make_pair(a,x));
    }sort(TC.begin(), TC.end(), check);
    vector< pair<int,int> > :: const_iterator i,j;
    //cout<<sum1<<" "<<sum2<<endl;
    for(i=CF.end()-1,j=TC.end()-1;i>=CF.begin()&&j>=TC.begin(); i--,j--){
        int chmID = i->first;
        int chmC  = i->second;
        int TopID = j->first;
        int TopC  = j->second;
        //cout<<chmC<<" "<<TopC<<endl;
        if(sum1==sum2)break;
        if(n==1&&sum1>sum2 || m==1 && sum1<sum2){cout<<sum1+sum2<<endl;return 0;}
        if(sum1>sum2){
            sum1 -= chmC;
            //if(sum1<sum2){sum1 += chmC;}
        }else{
            sum2 -= TopC;
            //if(sum2<sum1){sum2 += TopC;}
        }
        n--,m--;
    }
    cout<<sum1+sum2<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define sz(a) int((a).size())
#define typeof(x) __typeof__(x)
#define all(c) (c).begin(),(c).end()
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
//tr(container name,iterator name)
#define present(c,x) ((c).find(x) != (c).end())
using namespace std;
int main()
{
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
	std::ios::sync_with_stdio(false);
	int n,m;
	std::map<int,int> ch1,ch2;
//	int a[n],x[n],b[m],y[m];
//	vll a(n),x(n),b(m),y(m);
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		ch1[a]=b;
	}
	cin>>m;
	for (int i = 0; i <m; ++i)
	{
		int a,b;
		cin>>a>>b;
		ch2[a]=b;
	}
	map<int,int>::iterator it,it2;
	it2=ch2.begin();
	it=ch1.begin();
//	vector<pair<int,int> > same;
	vi same;
	while(it!=ch1.end() && it2!=ch2.end())
    {
    	if(it->first<it2->first)
    	{
    		it++;
    	}
    	else if(it->first>it2->first)
    	{
	    	it2++;
    	}
    	else
    	{
    		//same.pb(make_pair(it->first,it->second));
    		same.pb(it->first);
    		it++;
    		it2++;
    	}
    }
/*    for(int i=0;i<sz(same);i++)
    {
    	ch1.erase(same[i].f);
    	ch2.erase(same[i].f);
    	cout<<same[i].f<<" "<<same[i].s<<endl;
    }
*/
    ll sum1=0,sum2=0;
    for (it=ch1.begin(); it!=ch1.end(); ++it)
    {
    	if(find(same.begin(),same.end(),it->first)==same.end())
	    {
	    	sum1+=it->s;
	    }
    }
    for (it=ch2.begin(); it!=ch2.end(); ++it)
    {
    	if(find(same.begin(),same.end(),it->first)==same.end())
	    	sum2+=it->s;
    }
//	cout<<sum1<<" "<<sum2<<endl;
	for(int i=same.size()-1;i>=0;i--)
	{
		if(sum1<sum2)
		{
			sum1+=ch1[same[i]];
//			cout<<sum1<<" "<<sum2<<endl;
		}
		else
		{
			sum2+=ch2[same[i]];
//			cout<<sum1<<" "<<sum2<<endl;
		}
	}
	cout<<sum1+sum2<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define sz(a) int((a).size())
#define typeof(x) __typeof__(x)
#define all(c) (c).begin(),(c).end()
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
//tr(container name,iterator name)
#define present(c,x) ((c).find(x) != (c).end())
using namespace std;
int main()
{
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
	std::ios::sync_with_stdio(false);
	int n,m;
	std::map<int,int> ch1,ch2;
    unsigned long long int sum1=0,sum2=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		ch1[a]=b;
		sum1+=b;
	}
	cin>>m;
	for (int i = 0; i <m; ++i)
	{
		int a,b;
		cin>>a>>b;
		int sub = ch1[a];
		sum1-=sub;
		sub=max(sub,b);
		ch1[a]=sub;
		sum1+=sub;
	}
	cout<<sum1<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define sz(a) int((a).size())
#define typeof(x) __typeof__(x)
#define all(c) (c).begin(),(c).end()
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
//tr(container name,iterator name)
#define present(c,x) ((c).find(x) != (c).end())
using namespace std;
int main()
{
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
	std::ios::sync_with_stdio(false);
	int n,m;
	std::map<int,int> ch1,ch2;
//	int a[n],x[n],b[m],y[m];
//	vll a(n),x(n),b(m),y(m);
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		ch1[a]=b;
	}
	cin>>m;
	for (int i = 0; i <m; ++i)
	{
		int a,b;
		cin>>a>>b;
		ch2[a]=b;
	}
	map<int,int>::iterator it,it2;
	it2=ch2.begin();
	it=ch1.begin();
//	vector<pair<int,int> > same;
	vi same;
	while(it!=ch1.end() && it2!=ch2.end())
    {
    	if(it->first<it2->first)
    	{
    		it++;
    	}
    	else if(it->first>it2->first)
    	{
	    	it2++;
    	}
    	else
    	{
    		//same.pb(make_pair(it->first,it->second));
    		same.pb(it->first);
    		it++;
    		it2++;
    	}
    }
/*    for(int i=0;i<sz(same);i++)
    {
    	ch1.erase(same[i].f);
    	ch2.erase(same[i].f);
    	cout<<same[i].f<<" "<<same[i].s<<endl;
    }
*/
    ll sum1=0,sum2=0;
    for (it=ch1.begin(); it!=ch1.end(); ++it)
    {
    	if(find(same.begin(),same.end(),it->first)==same.end())
	    {
	    	sum1+=it->s;
	    }
    }
    for (it=ch2.begin(); it!=ch2.end(); ++it)
    {
    	if(find(same.begin(),same.end(),it->first)==same.end())
	    	sum2+=it->s;
    }
//	cout<<sum1<<" "<<sum2<<endl;
	for(int i=same.size()-1;i>=0;i--)
	{
		if(sum1<sum2)
		{
			sum1+=ch1[same[i]];
//			cout<<sum1<<" "<<sum2<<endl;
		}
		else if(sum2>sum1)
		{
			sum2+=ch2[same[i]];
//			cout<<sum1<<" "<<sum2<<endl;
		}
		else
		{
			if(ch1[same[i]]>ch2[same[i]])
			{
				sum1+=ch1[same[i]];
			}
			else
			{
				sum2+=ch2[same[i]];
			}
		}
	}
	cout<<sum1+sum2<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define sz(a) int((a).size())
#define typeof(x) __typeof__(x)
#define all(c) (c).begin(),(c).end()
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
//tr(container name,iterator name)
#define present(c,x) ((c).find(x) != (c).end())
using namespace std;
int main()
{
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
	std::ios::sync_with_stdio(false);
	int n,m;
	std::map<int,int> ch1,ch2;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int a,b;
		cin>>a>>b;
		ch1[a]=b;
	}
	cin>>m;
	for (int i = 0; i <m; ++i)
	{
		int a,b;
		cin>>a>>b;
		ch2[a]=b;
	}
	map<int,int>::iterator it,it2;
	it2=ch2.begin();
	it=ch1.begin();
	vi same;
	while(it!=ch1.end() && it2!=ch2.end())
    {
    	if(it->first<it2->first)
    		it++;
    	else if(it->first>it2->first)
	    	it2++;
    	else
    	{
    		same.pb(it->first);
    		it++;
    		it2++;
    	}
    }
    unsigned long long int sum1=0,sum2=0;
    for (it=ch1.begin(); it!=ch1.end(); ++it)
    {
    	if(find(same.begin(),same.end(),it->first)==same.end())
	    {
	    	sum1+=it->s;
	    }
    }
    for (it=ch2.begin(); it!=ch2.end(); ++it)
    {
    	if(find(same.begin(),same.end(),it->first)==same.end())
	    	sum2+=it->s;
    }
//	cout<<sum1<<" "<<sum2<<endl;
	for(int i=same.size()-1;i>=0;i--)
	{
		if(sum1+ch1[same[i]]+sum2>sum2+sum1+ch2[same[i]])
		{
			sum1+=ch1[same[i]];
//			cout<<sum1<<" "<<sum2<<endl;
		}
		else if(sum1+ch1[same[i]]+sum2>sum2+sum1+ch2[same[i]])
		{
			sum2+=ch2[same[i]];
//			cout<<sum1<<" "<<sum2<<endl;
		}
		else
		{
			if(ch1[same[i]]>ch2[same[i]])
			{
				sum1+=ch1[same[i]];
			}
			else
			{
				sum2+=ch2[same[i]];
			}
		}
	}
	cout<<sum1+sum2<<endl;
	return 0;
}


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

long long n , m , a , x , b , y , ans , temp ;
map <long long , long long> pt ;
bool vis[1000000000] ;
vector < pair < long long , long long > > ve ;

int main(){

	memset(vis,0,sizeof(vis));

	cin >> n ;

	for( long long i = 1 ; i <= n ; i++ ){

		cin >> a >> x ;
		pt[a] = x ;
		ve.push_back(make_pair(a,x)) ;

	}

	cin >> m ;
	temp = n ;
	for( int j = 1 ;  j <= m ; j++){

		cin >> b >> y ;

		int cek = pt[b] ;

		if(cek){
			temp--;
			vis[b]=1;
		}

		if( cek > y ) ans += cek ;
		else ans += y ;

	}

	if(temp>0){

		for(int i = 0 ; i < ve.size() ; i++ ){

			if(!vis[ve[i].first]) ans += ve[i].second ;

		}

	}

	cout << ans << endl ;

}


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

long long n , m , a , x , b , y , ans , temp ;
map <long long , long long> pt ;
bool vis[1100000000] ;
vector < pair < long long , long long > > ve ;

int main(){

	memset(vis,0,sizeof(vis));

	cin >> n ;

	for( long long i = 1 ; i <= n ; i++ ){

		cin >> a >> x ;
		pt[a] = x ;
		ve.push_back(make_pair(a,x)) ;

	}

	cin >> m ;
	temp = n ;
	for( int j = 1 ;  j <= m ; j++){

		cin >> b >> y ;

		int cek = pt[b] ;

		if(cek){
			temp--;
			vis[b]=1;
		}

		if( cek > y ) ans += cek ;
		else ans += y ;

	}

	if(temp>0){

		for(int i = 0 ; i < ve.size() ; i++ ){

			if(!vis[ve[i].first]) ans += ve[i].second ;

		}

	}

	cout << ans << endl ;

}


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

long long n , m , a , x , b , y , ans , temp ;
map <long long , long long> peta ;
bool vis[1100000000] ;
vector < pair < long long , long long > > ve ;

int main(){

	memset(vis,0,sizeof(vis));

	cin >> n ;

	for( long long i = 1 ; i <= n ; i++ ){

		cin >> a >> x ;
		peta[a] = x ;
		ve.push_back(make_pair(a,x)) ;

	}

	cin >> m ;
	temp = n ;
	for( int j = 1 ;  j <= m ; j++){

		cin >> b >> y ;

		int cek = peta[b] ;

		if(cek){
			temp--;
			vis[b]=1;
		}

		if( cek > y ) ans += cek ;
		else ans += y ;

	}

	if(temp>0){

		for(int i = 0 ; i < ve.size() ; i++ ){

			if(!vis[ve[i].first]) ans += ve[i].second ;

		}

	}

	cout << ans << endl ;

}


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

long long ans , n , m , a , x , b , y ;

map < long long , long long > pt ;

int main(){

	cin >> n ;

	for( int i = 0 ; i < n ; i++ ){

		cin >> a >> x ;
		pt[a] = x ;

		ans += x ;

	}

	cin >> m ;

	for( int i = 0 ; i < m ; i++ ){

		cin >> b >> y ;
		long long cek = pt[b] ;

		if( cek ){

			if( cek < y ) ans = ans + y - cek ;

		}
		if( cek == 0 ) ans += y ;

	}

	cout << ans << endl ;

}


#include <iostream>
#include <map>

int main() {
    long long n_a, n_b;

    std::cin >> n_a;

    std::map< long long,  long long> chemicals;

    for(int i = 0; i < n_a; i++) {
        int a,b;
        std::cin >> a >> b;

        chemicals[a] = b;
    }

    std::cin >> n_b;

    for(int i = 0; i < n_b; i++) {
        int a,b;
        std::cin >> a >> b;
        std::map< long long,  long long>::iterator it;
        it = chemicals.find(a);
        if( it != chemicals.end()) {
            if(it->second < b) {
                it->second = b;
            }
        } else {
            chemicals[a] = b;
        }
    }

    int sum = 0;

    for(auto &it: chemicals) {
        sum += it.second;
    }

    std::cout << sum << std::endl;
    return 0;
}


#include <iostream>
#include <map>

int main() {
    int n_a, n_b;

    std::cin >> n_a;

    std::map<int, int> chemicals;

    for(int i = 0; i < n_a; i++) {
        int a,b;
        std::cin >> a >> b;

        chemicals[a] = b;
    }

    std::cin >> n_b;

    for(int i = 0; i < n_b; i++) {
        int a,b;
        std::cin >> a >> b;
        std::map<int, int>::iterator it;
        it = chemicals.find(a);
        if( it != chemicals.end()) {
            if(it->second < b) {
                it->second = b;
            }
        } else {
            chemicals[a] = b;
        }
    }

    int sum = 0;

    for(auto &it: chemicals) {
        sum += it.second;
    }

    std::cout << sum << std::endl;
    return 0;
}


#include <iostream>
#include <map>

int main() {
    long long n_a, n_b;

    std::cin >> n_a;

    std::map< long long,  long long> chemicals;

    for(int i = 0; i < n_a; i++) {
        long long  a,b;
        std::cin >> a >> b;

        chemicals[a] = b;
    }

    std::cin >> n_b;

    for(int i = 0; i < n_b; i++) {
        long long  a,b;
        std::cin >> a >> b;
        std::map< long long,  long long>::iterator it;
        it = chemicals.find(a);
        if( it != chemicals.end()) {
            if(it->second < b) {
                it->second = b;
            }
        } else {
            chemicals[a] = b;
        }
    }

    long long  sum = 0;

    for(auto &it: chemicals) {
        sum += it.second;
    }

    std::cout << sum << std::endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long ans;
int x,y,n,m;
map<int,int>ma;
int main(){
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>x>>y,ma[x]=y,ans+=y;
    cin>>m;
    for(int i=0; i<m; i++)
    {
        cin>>x>>y;
        if(ma[x]<y)
        {
            ans-=ma[x];
            ans+=y;
        }
    }
    cout<<ans;
    return 0;
}


#include<stdio.h>
#include<string.h>

int a[1000000005];

int main(){
	int i,n,m,x,y;
	memset(a,0,sizeof(a));
	__int64 ans=0;
	scanf("%d",&n);
	for (i=0;i<n;i++) {
		scanf("%d%d",&x,&y);
		ans+=y;
		a[x]=y;
	}
	scanf("%d",&m);
	for (i=0;i<m;i++) {
		scanf("%d%d",&x,&y);
		if (a[x]<y) ans+=y-a[x];
	}
	printf("%I64d",ans);
	return 0;
}


                    #include<iostream>
                    #include<map>
                    #define long long ll;
                    using namespace std;
                    int main()
                    {map<ll ,ll>m;
                          ll n,p,c=0;
                          cin>>n;
                          while(n--)
                          {
                            ll a,b;
                             cin>>a>>b;
                             m[a]=b;c+=b;
                          }cin>>p;
                          while(p--)
                          {
                         ll a,b;
                          cin>>a>>b;
                          if(m[a]<b)
                          c+=b-m[a];

                          }

                        cout  <<c;
                    }


                        #include<iostream>
                        #include<map>

                        using namespace std;
                        int main()
                        {map<long long ,long long>m;
                              long long n,p,c=0;
                              cin>>n;
                              while(n--)
                              {
                                long long  a,b;
                                 cin>>a>>b;
                                 m[a]=b;c+=b;
                              }cin>>p;
                              while(p--)
                              {
                         long long  a,b;
                              cin>>a>>b;
                              if(m[a]<b)
                              c+=b-m[a];

                              }

                            cout  <<c;
                        }


                #include<iostream>
                #include<map>
                using namespace std;
                int main()
                {map<int ,int>m;
                      int n,p,c=0;
                      cin>>n;
                      while(n--)
                      {
                         int a,b;
                         cin>>a>>b;
                         m[a]=b;c+=b;
                      }cin>>p;
                      while(p--)
                      {
                      int a,b;
                      cin>>a>>b;
                      if(m[a]<b)
                      c=b-m[a];

                      }

                    cout  <<c;
                }


    #include<iostream>
    #include<map>
    using namespace std;
    int main()
    {map<int ,int>m;
          int n,m,c=0;
          cin>>n;
          while(n--)
          {
             int a,b;
             cin>>a>>b;
             m[a]=b;
          }cin>>m;
          while(m--)
          {
          int a,b;
          cin>>a>>b;
          if(m[a]<b)
          m[a]=b;
          }
        auto it=m.begin();
        if(!it==m.end)
        {
             c+=it.second();
        }
        cout  <<c;
    }


    #include<iostream>
    #include<map>
    using namespace std;
    int main()
    {map<int ,int>m;
          int n,p,c=0;
          cin>>n;
          while(n--)
          {
             int a,b;
             cin>>a>>b;
             m[a]=b;
          }cin>>p;
          while(p--)
          {
          int a,b;
          cin>>a>>b;
          if(m[a]<b)
          m[a]=b;
          }
        auto it=m.begin();
        if(!it==m.end())
        {
             c+=it.second;
        }
        cout  <<c;
    }


    #include<iostream>
    #include<map>
    using namespace std;
    int main()
    {map<int ,int>m;
          int n,p,c=0;
          cin>>n;
          while(n--)
          {
             int a,b;
             cin>>a>>b;
             m[a]=b;
          }cin>>p;
          while(p--)
          {
          int a,b;
          cin>>a>>b;
          if(m[a]<b)
          m[a]=b;
          }
        auto it=m.begin();
        if(!(it==m.end()))
        {
             c+=it.second();
        }
        cout  <<c;
    }


    #include<iostream>
    #include<map>
    using namespace std;
    int main()
    {map<int ,int>m;
          int n,p,c=0;
          cin>>n;
          while(n--)
          {
             int a,b;
             cin>>a>>b;
             m[a]=b;
          }cin>>p;
          while(p--)
          {
          int a,b;
          cin>>a>>b;
          if(m[a]<b)
          m[a]=b;
          }
       map<int,int>::iterator it=m.begin();
       {
             while(it!=m.end()
             {
                   c+=it.second;
             }
       }
        cout  <<c;
    }


            #include<iostream>
            #include<map>
            using namespace std;
            int main()
            {map<int ,int>m;
                  int n,p,c=0;
                  cin>>n;
                  while(n--)
                  {
                     int a,b;
                     cin>>a>>b;
                     m[a]=b;
                  }cin>>p;
                  while(p--)
                  {
                  int a,b;
                  cin>>a>>b;
                  if(m[a]<b)
                  m[a]=b;
                  }
               map<int,int>::iterator it=m.begin();
               {
                     while(it!=m.end())
                     {
                           c+=it->second;
                     }
               }
                cout  <<c;
            }


    #include<iostream>
    #include<map>
    using namespace std;
    int main()
    {map<int ,int>m;
          int n,p,c=0;
          cin>>n;
          while(n--)
          {
             int a,b;
             cin>>a>>b;
             m[a]=b;
          }cin>>p;
          while(p--)
          {
          int a,b;
          cin>>a>>b;
          if(m[a]<b)
          m[a]=b;
          }
        auto it=m.begin();
        if(!it==m.end)
        {
             c+=it.second;
        }
        cout  <<c;
    }


#include<iostream>
#include<map>
using namespace std;
int main()
{map<int ,int>m;
      int n,m,c=0;
      cin>>n;
      while(n--)
      {
         int a,b;
         cin>>a>>b;
         m[a]=b;
      }cin>>m;
      while(m--)
      {
      int a,b;
      cin>>a>>b;
      if(m[a]<b)
      m[a]=b;
      }
    auto it=m.begin();
    if(!it==m.end)
    {
         c+=it.second;
    }
    cout  <<c;
}


                    #include<iostream>
                    #include<map>
                    using namespace std;
                    int main()
                    {map<int ,int>m;
                          int n,p,c=0;
                          cin>>n;
                          while(n--)
                          {
                             int a,b;
                             cin>>a>>b;
                             m[a]=b;c+=b;
                          }cin>>p;
                          while(p--)
                          {
                          int a,b;
                          cin>>a>>b;
                          if(m[a]<b)
                          c+=b-m[a];

                          }

                        cout  <<c;
                    }


        #include<iostream>
        #include<map>
        using namespace std;
        int main()
        {map<int ,int>m;
              int n,p,c=0;
              cin>>n;
              while(n--)
              {
                 int a,b;
                 cin>>a>>b;
                 m[a]=b;
              }cin>>p;
              while(p--)
              {
              int a,b;
              cin>>a>>b;
              if(m[a]<b)
              m[a]=b;
              }
           map<int,int>::iterator it=m.begin();
           {
                 while(it!=m.end())
                 {
                       c+=it.second;
                 }
           }
            cout  <<c;
        }


    #include<iostream>
    #include<map>
    using namespace std;
    int main()
    {map<int ,int>m;
          int n,p,c=0;
          cin>>n;
          while(n--)
          {
             int a,b;
             cin>>a>>b;
             m[a]=b;
          }cin>>p;
          while(p--)
          {
          int a,b;
          cin>>a>>b;
          if(m[a]<b)
          m[a]=b;
          }
       map<int,int>::iterator it=m.begin();
        if(!it==m.end)
        {
             c+=it.second;
             it++;
        }
        cout  <<c;
    }


    #include<iostream>
    #include<map>
    using namespace std;
    int main()
    {map<int ,int>m;
          int n,m,c=0;
          cin>>n;
          while(n--)
          {
             int a,b;
             cin>>a>>b;
             m[a]=b;
          }cin>>m;
          while(m--)
          {
          int a,b;
          cin>>a>>b;
          if(m[a]<b)
          m[a]=b;
          }
        auto it=m.begin();
        if(!it==m.end())
        {
             c+=it.second;
        }
        cout  <<c;
    }


import java.io.*;
import java.util.*;
public class Main
{

	private void solve()throws IOException
	{
		int n=nextInt();
		int degree[]=new int[n+1];
		for(int i=1;i<n;i++)
		{
			int u=nextInt();
			int v=nextInt();
			degree[u]++;
			degree[v]++;
		}
		int root=0;
		ArrayList<Integer> leaves=new ArrayList<>();
		for(int i=1;i<=n;i++)
		{
			if(degree[i]>2)
			{
				if(root==0)
					root=i;
				else
				{
					out.println("No");
					return;
				}
			}
			if(degree[i]==1)
				leaves.add(i);
		}
		out.println("Yes\n"+(leaves.size()+1)/2);
		for(int i=0;i<leaves.size();i+=2)
		{
			if(i==leaves.size()-1)
				out.println(leaves.get(i)+" "+root);
			else
				out.println(leaves.get(i)+" "+leaves.get(i+1));
		}
	}

	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();

		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}


#include <bits/stdc++.h>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
const int maxn = 1e6 + 1000;
void solve() {};
struct EDGE {
	int to, next;
}edges[maxn << 1];
struct RES {
	int u, v;
} res[maxn];
int n, first[maxn], edgesNum = 0 ;
int top = 0;
bool vis[maxn], vis1[maxn];
int in[maxn], out[maxn];
int cnt[maxn], ans = 0, id;
void addEdge(int u, int v) {
	edges[edgesNum].to = v;
	edges[edgesNum].next = first[u];
	first[u] = edgesNum++;
}
void dfs(int u, bool fff) {
	vis[u] = 1;
	bool f = 0, judge = 0;
	for(int i = first[u]; ~i; i = edges[i].next) {
		int v = edges[i].to;
		if(!vis[v]) {
			if(!fff) {
				cnt[u]++;
				if(!f && cnt[u] > 1) {
					f = 1;
					id = u;
					ans++;
				}
			}
			dfs(v, fff);
			judge = 1;
		}
	}
	if(fff && !judge && !vis1[u]) {
		//cout << id << "*** " << u << endl;
		res[top].u = min(id, u);
		res[top++].v = max(id, u);
		vis1[u] = 1;
	}
}
bool cmp(RES a, RES b) {
	if(a.u == b.u) return a.v < b.v;
	return a.u < b.u;
}
int main() {
	while(~scanf("%d", &n)) {
		met(cnt, 0);
		met(vis, 0);
		met(vis1, 0);
		met(in, 0);
		met(out, 0);
		met(first, -1);
		ans = 0;
		top = 0;
		id = -1;
		edgesNum = 0;
		int u, v;
		for(int i = 0; i < n - 1; i++) {
			scanf("%d%d", &u, &v);
			in[v]++;
			in[u]++;
			out[v]++;
			out[v]++;
			addEdge(u, v);
			addEdge(v, u);
		}
		int tar;
		for(int i = 1; i <= n; i++) {
			if(in[i] == 1) {
				tar = i;
				break;
			}
		}
		dfs(tar, 0);
		if(ans <= 1) {
			puts("Yes");
			met(vis, 0);
			if(id != -1)
				dfs(id, 1);
			else {
				id = 1;
				dfs(1, 1);
			}
			//sort(res, res + top, cmp);
			printf("%d\n", top);
			for(int i = 0; i < top; i++) {
				printf("%d %d\n", res[i].u, res[i].v);
			}
		}
		else puts("No");
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <unordered_map>
#include <cstdio>
#include <set>
using namespace std;
unordered_map <int, bool> visited;
set <pair<int, int>> path;
vector<vector<int>> t;
int n;
bool found = false;
bool check(vector<vector<int>> &t){
  int cnt = 0;
  for(auto p: t){
    if(p.size() > 2) cnt++;
  }
  return cnt <= 1? true: false;
}
int findroot(vector<vector<int>> &t){
  int root = 1;
  for(int i = 1; i <= n; i++){
    if(t[i].size() > t[root].size()){
      root = i;
      found = true;
    }
  }
  return root;
}
void solve(vector<vector<int>> &t, int n){
  vector <int> leaves;
  for(int i = 1; i < t.size(); i++){
    if(t[i].size() == 1) leaves.push_back(i);
  }
  printf("%d\n", int(leaves.size()));
  for(auto e: leaves){
    printf("%d %d\n", n, e);
  }
}

int main(){
  cin >> n;
  t.resize(n + 1);
  for(int i = 0; i < n - 1; i++){
    int x, y; cin >> x >> y;
    if(n == 2){
      puts("Yes");
      cout << 1 << endl << x << ' ' << y;
      return 0;
    }
    t[x].push_back(y);
    t[y].push_back(x);
  }
  if(!check(t)){
    puts("No");
    return 0;
  }
  puts("Yes");
  solve(t, findroot(t));
  return 0;
}