import java.util.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Kotlin {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner (System.in);
		PrintWriter pw = new PrintWriter (System.out);
		int t = sc.nextInt();
		for (int p = 0 ; p < t ; p ++ ) {
			int l = sc.nextInt();

			int [] arr = new int [l]  ; int []  arr2 = new int [l] ;
			for (int i = 0  ;  i < l ;  i ++ ) {
				arr[i] = sc.nextInt();
				arr2[i] = sc.nextInt();
			}if (l == 1 ) {pw.println(0) ; }else {
			Arrays.sort(arr);
			Arrays.sort(arr2);
			if (arr[l-1] <= arr2[0] && arr2[l-1] <= arr[0]) {pw.println(0);}else {
			int n = Math.abs(arr2[0] - arr[l-1]) ;
			int m = Math.abs(arr2[l-1] - arr[0]) ;
			int max = (n > m) ? m : n ;
			pw.println(max);

		}}}
		pw.close();
	}
	static class Scanner {
		StringTokenizer st;
		BufferedReader br;
		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}
		public Scanner(String file) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}

}


import java.util.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Kotlin {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner (System.in);
		PrintWriter pw = new PrintWriter (System.out);
		int t = sc.nextInt();
		for (int p = 0 ; p < t ; p ++ ) {
			int l = sc.nextInt();

			int [] arr = new int [l]  ; int []  arr2 = new int [l] ;
			for (int i = 0  ;  i < l ;  i ++ ) {
				arr[i] = sc.nextInt();
				arr2[i] = sc.nextInt();
			}if (l == 1 ) {pw.println(0) ; }else {
			Arrays.sort(arr);
			Arrays.sort(arr2);
			int n = Math.abs(arr2[0] - arr[l-1]) ;
			int m = Math.abs(arr2[l-1] - arr[0]) ;
			int max = (n > m) ? m : n ;
			pw.println(max);

		}}
		pw.close();
	}
	static class Scanner {
		StringTokenizer st;
		BufferedReader br;
		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}
		public Scanner(String file) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}

}


package main;

import java.util.Scanner;
public class Main {
	final static int N=(int)(1e5+10);
	static int t;
	static String s;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		for(int i =0;i<t;i++) {
			int n=sc.nextInt();
			if(n==1) {
				System.out.println(0);
				break;
			}
			int min=(int)(1e9);
			int max=0;
			int[] a=new int[N];
			int[] b=new int[N];
			for(int j=0;j<n;j++) {
				a[j]=sc.nextInt();
				b[j]=sc.nextInt();
				max=a[j]>max? a[j]:max;
				min=b[j]<min? b[j]:min;
			}
			System.out.println((int)Math.abs(max-min));
		}
	}

}


import java.util.Scanner;
public class Main {
	final static int N=(int)(1e5+10);
	static int t;
	static String s;
	static int[] a=new int[N];
	static int[] b=new int[N];
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		for(int i =0;i<t;i++) {
			int n=sc.nextInt();

			int min=(int)(1e9);
			int max=0;
			for(int j=0;j<n;j++) {
				a[j]=sc.nextInt();
				b[j]=sc.nextInt();
				max=a[j]>max? a[j]:max;
				min=b[j]<min? b[j]:min;
			}
			if(n==1) {
				System.out.println(0);
				continue;
			}
			System.out.println((max-min)>0? max-min:0);
		}
	}

}


import java.util.Scanner;
public class Main {
	final static int N=(int)(1e5+10);
	static int t;
	static String s;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		for(int i =0;i<t;i++) {
			int n=sc.nextInt();
			if(n==1) {
				System.out.println(0);
				continue;
			}
			int min=(int)(1e9);
			int max=0;
			int[] a=new int[N];
			int[] b=new int[N];
			for(int j=0;j<n;j++) {
				a[j]=sc.nextInt();
				b[j]=sc.nextInt();
				max=a[j]>max? a[j]:max;
				min=b[j]<min? b[j]:min;
			}
			System.out.println((int)Math.abs(max-min));
		}
	}

}


import java.util.Scanner;
public class Main {
	final static int N=(int)(1e5+10);
	static int t;
	static String s;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		for(int i =0;i<t;i++) {
			int n=sc.nextInt();
			if(n==1) {
				System.out.println(0);
				break;
			}
			int min=(int)(1e9);
			int max=0;
			int[] a=new int[N];
			int[] b=new int[N];
			for(int j=0;j<n;j++) {
				a[j]=sc.nextInt();
				b[j]=sc.nextInt();
				max=a[j]>max? a[j]:max;
				min=b[j]<min? b[j]:min;
			}
			System.out.println((int)Math.abs(max-min));
		}
	}

}


import java.io.PrintWriter;
import java.util.Scanner;

public class MathProblem {
    static Scanner sc = new Scanner(System.in);
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String args[]) {
        int t = sc.nextInt();
        while (t-- > 0) {
            solve();
        }
    }

    private static void solve() {
        int n = sc.nextInt();
        int leftEnd = Integer.MAX_VALUE;
        int rightStart = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            leftEnd = Math.min(leftEnd, r);
            rightStart = Math.max(rightStart, l);
        }

        int ans = 0;
        if (leftEnd >= rightStart) {
            ans = 0;
        } else {
            ans = rightStart - leftEnd;
        }

        System.out.println(ans);
    }
}


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by Ринат on 26.09.2018.
 */
public class B {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int n = in.nextInt();
        for (int i = 0; i <n; i++) {
            int m = in.nextInt();
            int[]a = new int[m];
            int[]b = new int[m];
            for (int j = 0; j <m ; j++) {
                a[j] = in.nextInt();
                b[j] = in.nextInt();
            }
            Arrays.sort(a);
            Arrays.sort(b);
            int x = a[m-1]-b[0];
            if(x<0)x=0;
            out.println(x);
        }
        out.close();
    }
}


import java.util.*;
import java.io.*;
public final class Codechef {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0) {
            int n = sc.nextInt();
            int x = 0;
            int y = Integer.MAX_VALUE;
            for(int i = 0; i<n; i++) {
                x = Math.max(x, sc.nextInt());
                y = Math.min(y, sc.nextInt());
            }
            System.out.println(Math.max(x-y, 0));
        }
    }
}


import java.util.Scanner;

public class Jonny {
static  Scanner in=new Scanner(System.in);
public static void main(String args[]){
	int t=in.nextInt();
	for(int i=0;i<t;i++) {
		int n=in.nextInt();
		int[][] a=new int[n][2];
		for(int j=0;j<n;j++)
			for(int k=0;k<2;k++)a[j][k]=in.nextInt();
		//Arrays.sort(a, Comparator.comparingInt(arr -> arr[1]));
		int max=0; int min=a[0][1];
		for(int j=0;j<n;j++) {
			if(a[j][1]<min)min=a[j][1];
		if(a[j][0]>max)max=a[j][0];
		}
		if(max-min<0)System.out.println(0);
		else System.out.println(max-min);
	}

}}


import java.awt.event.MouseAdapter;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        new Main().run();
    }

        int p = 0,l;
        char a[];
    long readLong(){
        long s = 0;
        while(p<l&&a[p]>='0'&&a[p]<='9'){
           s = s*10L+ (a[p]-'0');  p++;
        }
        return s;

    }

    void solve() {
//        String n = ns();
//        int x = ni();
//
//        if(x>0){
//
//            for(int i=0;i< n.length();++i){
//
//            }
//
//
//
//
//        }

    int trie[][] = new int[4500][10];
    int d[] = new int[4500];
    Arrays.fill(d,10000001);

    int ct = 1;

   int t =ni();
   while(t-->0){
       int n = ni();
       int p[][] = new int[n][2];
       for(int i=0;i<n;++i)
       {
           p[i][0] = ni();
           p[i][1] = ni();
       }
       Arrays.sort(p,(int[] x,int[] y)->{
          return x[1]-y[1];
       });

       int a = p[0][1];
        int b = a;
       for(int i=1;i<n;++i){
           b = Math.max(b,p[i][0]);
       }
       println(b-a);

   }

//
//        for(int i=0;i<1000000;++i){
//            r = mul(r,a,k);
//            if(Arrays.equals(r,a)){
//                println(i+1);return;
//            }
//        }
//        println(-1);

//        int n = ni();
//        int p = ni();
//
//        int h[] = new int[n+1];
//        Arrays.fill(h,-1);
//        int to[] = new int[2*n+5];
//        int ne[] = new int[2*n+5];
//        int ct = 0;
//
//        for(int i=0;i<p;i++){
//            int x = ni();
//            int y = ni();
//            to[ct] = x;
//            ne[ct] = h[y];
//            h[y] = ct++;
//
//            to[ct] = y;
//            ne[ct] = h[x];
//            h[x] = ct++;
//
//        }
//
//        println(go(1,h,ne,to,-1));

        //        int n= ni();
//        //int m = ni();
//        int  l = 2*n;
//
//        String s[] = new String[2*n+1];
//
//        long a[] = new long[2*n+1];
//        for(int i=1;i<=n;++i){
//            s[i] = ns();
//            s[i+n] = s[i];
//            a[i] = ni();
//            a[i+n] = a[i];
//        }
//
//        long dp[][] = new long[l+1][l+1];
//        long dp1[][] = new long[l+1][l+1];
//
//        for(int i = l;i>=1;--i) {
//
//            Arrays.fill(dp[i],-1000000000);
//            Arrays.fill(dp1[i],1000000000);
//        }
//
//        for(int i = l;i>=1;--i) {
//            dp[i][i] = a[i];
//            dp1[i][i] = a[i];
//        }
//
//
//
//        for(int i = l;i>=1;--i) {
//
//            for (int j = i+1; j <= l&&j-i+1<=n; ++j) {
//
//
//                for(int e=i;e<j;++e){
//                    if(s[e+1].equals("t")){
//                        dp[i][j] = Math.max(dp[i][j], dp[i][e]+dp[e+1][j]);
//                        dp1[i][j] = Math.min(dp1[i][j], dp1[i][e]+dp1[e+1][j]);
//                    }else{
//
//                        long f[] = {dp[i][e]*dp[e+1][j],dp1[i][e]*dp1[e+1][j],dp[i][e]*dp1[e+1][j],dp1[i][e]*dp[e+1][j]};
//
//                        for(long u:f) {
//                            dp[i][j] = Math.max(dp[i][j], u);
//                            dp1[i][j] = Math.min(dp1[i][j], u);
//                        }
//                    }
//
//
//                }
//
//            }
//        }
//        long ma = -100000000;
//        List<Integer> li = new ArrayList<>();
//        for (int j = 1; j <= n; ++j) {
//            if(dp[j][j+n-1]==ma){
//                li.add(j);
//            }else if(dp[j][j+n-1]>ma){
//                ma = dp[j][j+n-1];
//                li.clear();
//                li.add(j);
//            }
//
//        }

//        println(ma);
//        for(int u:li){
//            print(u+" ");
//        }
//        println();

//        println(get(490));

//        int num =1;
//      while(true) {
//          int n = ni();
//          int m = ni();
//          if(n==0&&m==0) break;
//          int p[] = new int[n];
//          int d[] = new int[n];
//          for(int j=0;j<n;++j){
//              p[j] = ni();
//              d[j] = ni();
//          }
//          int dp[][] = new int[8001][22];
//          int choose[][] = new int[8001][22];
//
//          for(int v=0;v<=8000;++v){
//              for(int u=0;u<=21;++u) {
//                  dp[v][u] = -100000;
//                  choose[v][u] =-1;
//              }
//          }
//          dp[4000][0] = 0;
//
//          for(int j=0;j<n;++j){
//             for(int g = m-1 ;g>=0; --g){
//                 if(p[j] - d[j]>=0) {
//                     for (int v = 4000; v >= -4000; --v) {
//                         if (v + 4000 + p[j] - d[j] >= 0 && v + 4000 + p[j] - d[j] <= 8000 && dp[v + 4000][g] >= 0) {
//                             int ck1 = dp[v + 4000 + p[j] - d[j]][g + 1];
//                             if (ck1 < dp[v + 4000][g] + p[j] + d[j]) {
//                                 dp[v + 4000 + p[j] - d[j]][g + 1] = dp[v + 4000][g] + p[j] + d[j];
//                                 choose[v + 4000 + p[j] - d[j]][g + 1] = j;
//                             }
//                         }
//
//                     }
//                 }else{
//                     for (int v = -4000; v <= 4000; ++v) {
//                         if (v + 4000 + p[j] - d[j] >= 0 && v + 4000 + p[j] - d[j] <= 8000 && dp[v + 4000][g] >= 0) {
//                             int ck1 = dp[v + 4000 + p[j] - d[j]][g + 1];
//                             if (ck1 < dp[v + 4000][g] + p[j] + d[j]) {
//                                 dp[v + 4000 + p[j] - d[j]][g + 1] = dp[v + 4000][g] + p[j] + d[j];
//                                 choose[v + 4000 + p[j] - d[j]][g + 1] = j;
//                             }
//                         }
//
//                     }
//
//
//
//
//
//                 }
//             }
//          }
//          int big = 0;
//          int st = 0;
//          boolean ok = false;
//          for(int v=0;v<=4000;++v){
//              int v1 = -v;
//              if(dp[v+4000][m]>0){
//                  big = dp[v+4000][m];
//                  st = v+4000;
//                  ok = true;
//              }
//              if(dp[v1+4000][m]>0&&dp[v1+4000][m]>big){
//                  big = dp[v1+4000][m];
//                  st = v1+4000;
//                  ok = true;
//              }
//              if(ok){
//                  break;
//              }
//          }
//          int f = 0;
//          int s = 0;
//          List<Integer> res = new ArrayList<>();
//          while(choose[st][m]!=-1){
//              int j = choose[st][m];
//              res.add(j+1);
//              f += p[j];
//              s += d[j];
//              st -= p[j]-d[j];
//              m--;
//          }
//          Collections.sort(res);
//          println("Jury #"+num);
//          println("Best jury has value " + f + " for prosecution and value " + s + " for defence:");
//          for(int u=0;u<res.size();++u){
//              print(" ");
//              print(res.get(u));
//          }
//          println();
//          println();
//          num++;
//      }

//    int n = ni();
//    int m = ni();
//
//    int dp[][] = new int[n][4];
//
//    for(int i=0;i<n;++i){
//        for(int j=0;j<m;++j){
//            for(int c = 0;c<4;++c){
//                if(c==0){
//                    dp[i][j][] =
//                }
//            }
//        }
//    }

    }

    static void pushdown(int num, int le, int ri) {

    }

    long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    InputStream is;
    PrintWriter out;

    void run() throws Exception {
        is = System.in;
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private char ncc() {
        int b = readByte();
        return (char) b;
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private String nline() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!isSpaceChar(b) || b == ' ') {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[][] nm(int n, int m) {
        char[][] a = new char[n][];
        for (int i = 0; i < n; i++) a[i] = ns(m);
        return a;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private long[] nal(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++) a[i] = nl();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) {
        }
        ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while (true) {
            if (b >= '0' && b <= '9') num = (num << 3) + (num << 1) + (b - '0');
            else return minus ? -num : num;
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) {
        }
        ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        while (true) {
            if (b >= '0' && b <= '9') num = num * 10 + (b - '0');
            else return minus ? -num : num;
            b = readByte();
        }
    }

    void print(Object obj) {
        out.print(obj);
    }

    void println(Object obj) {
        out.println(obj);
    }

    void println() {
        out.println();
    }
}


import java.util.Scanner;

/**
 *
 * @author Hp
 */
public class JavaApplication141 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
          Scanner in = new Scanner(System.in);

	      int t= in.nextInt();

               while(t-->0){
            int n=in.nextInt();

            long min = 1000000000;  int max = 0;

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

                int x = in.nextInt() ;
                max = Math.max(max, x); //4

                int y = in.nextInt() ;
                min = Math.min(min,y ); //0
            }

            System.out.println(Math.max(max-min,0));
        }}}


import java.io.*;
import java.util.*;

public class __Solution {

    static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    public static void main(String[] args) {
        new __Solution().run();
    }

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    void init() throws FileNotFoundException {

        if (ONLINE_JUDGE) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }

        tok = new StringTokenizer("");
    }

    void run() {
        try {
            long timeStart = System.currentTimeMillis();

            init();
            solve();

            out.close();

            long timeEnd = System.currentTimeMillis();
            System.err.println("Time = " + (timeEnd - timeStart));
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    String readLine() throws IOException {
        return in.readLine();
    }

    String delimiter = " ";

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            String nextLine = readLine();
            if (null == nextLine) return null;

            tok = new StringTokenizer(nextLine);
        }

        return tok.nextToken(delimiter);
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    void solve() throws IOException {
    int t = readInt();
    for(int j = 0; j < t; ++j){
        int n = readInt();
        int amn = readInt();
        int bmx = readInt();
        for(int i = 1; i < n; ++i){
            int a = readInt();
            int b = readInt();
            if(a > amn){
                amn = a;
            }
            if(b < bmx){
                bmx = b;
            }
        }
        if(amn - bmx < 0){
            out.println(0);
        }
        else{
            out.println(amn - bmx);
        }
    }
    }
}


import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;
import java.io.*;
		public final class Solution {
		static PrintWriter out = new PrintWriter(System.out);
		static void flush() {
	 	out.flush();
	 	}
		static class FastReader {
	    BufferedReader br;
	    StringTokenizer st;

	    public FastReader() {
	    br = new BufferedReader(new
	    InputStreamReader(System.in));
	    }
	    static boolean isPalindrome(String str1, String str2) {
	    String str3 = str1+str2;
	    int i = 0, j = str3.length()-1;
	    while(i < j) {
	    char a = str3.charAt(i), b = str3.charAt(j);
	    if(a != b) return false;
	    i++;j--;
	    }
	    return true;
	    }
	    static boolean isPalindrome(String str) {
	    int i = 0, j = str.length()-1;
	    while(i < j) {
	    char a = str.charAt(i), b = str.charAt(j);
	    if(a != b) return false;
	    i++;j--;
	    }
	    return true;
	    }
	    String next() {
	    while (st == null || !st.hasMoreElements()) {
	    try{st = new StringTokenizer(br.readLine());}
	    catch (IOException  e) {e.printStackTrace();}
	    }
	    return st.nextToken();
	    }
	    int nextInt(){ return Integer.parseInt(next()); }
	    long nextLong() { return Long.parseLong(next());}
	    static int fact(int n) {
	    if(n == 1) return 1;
	    return n * fact(n-1);
	   	}
	    public int[] readIntArray(int n) {
	    int[] arr = new int[n];
		for(int i=0; i<n; ++i)
		arr[i]=nextInt();
		return arr;
		}
	    public int[][] readIntArray(int m, int n){
		int[][] arr = new int[m][n];
		for(int i = 0;i<m;i++)
		for(int j = 0;j<n;j++)
		arr[i][j] = nextInt();
		return arr;
	    }
	    public String[] readStringArray(int n) {
		String[] arr = new String[n];
		for(int i=0; i<n; ++i)
		arr[i]= nextLine();
		return arr;
	    }
		static int gcd(int a, int b) {
		if (b == 0) return a;
		return gcd(b, a % b);
		}
	    double nextDouble() {return Double.parseDouble(next());}
	    String nextLine() {
	    String str = "";
	    try{str = br.readLine();}
	    catch (IOException e) {e.printStackTrace();}
	    return str;}
	    }

		static int solve(int[] l, int[] r, int n) {
			int x = Integer.MAX_VALUE, y = Integer.MIN_VALUE, min = Integer.MAX_VALUE, max = -1;
//			if(n == 1) {
//				return 0;
//			}
			for(int i = 1;i<n;i++) {
				min = Math.min(min, r[i-1]);
				max = Math.max(max, l[i-1]);
				if(l[i] > min) {
					x = Math.min(x, min);
					y = Math.max(y, l[i]);
				}
				else if(r[i] < max) {
					x = Math.min(x, r[i]);
					y = Math.max(y, max);
				}
			}
			if(x == Integer.MAX_VALUE && y == Integer.MIN_VALUE){
				return 0;
			}
			return (Math.abs(y-x));
		}
		public static void main(String args[]) throws Exception {
	    FastReader sc = new FastReader();
	    long start = System.currentTimeMillis();
	    int t = sc.nextInt();

	    while(t--  > 0 ) {
	    	int n = sc.nextInt();
	    	int[] l = new int[n];
	    	int[] r = new int[n];
	    for(int i =0;i<n;i++) {
	    		l[i]= sc.nextInt();
	    		r[i] = sc.nextInt();
	    }
	    out.println(solve(l,r,n));
	    }
	        	flush();
	    long end = System.currentTimeMillis();
	    NumberFormat formatter = new DecimalFormat("#0.00000");
	    //System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
	    }
	}


import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;
import java.io.*;
		public final class Solution {
		static PrintWriter out = new PrintWriter(System.out);
		static void flush() {
	 	out.flush();
	 	}
		static class FastReader {
	    BufferedReader br;
	    StringTokenizer st;

	    public FastReader() {
	    br = new BufferedReader(new
	    InputStreamReader(System.in));
	    }
	    static boolean isPalindrome(String str1, String str2) {
	    String str3 = str1+str2;
	    int i = 0, j = str3.length()-1;
	    while(i < j) {
	    char a = str3.charAt(i), b = str3.charAt(j);
	    if(a != b) return false;
	    i++;j--;
	    }
	    return true;
	    }
	    static boolean isPalindrome(String str) {
	    int i = 0, j = str.length()-1;
	    while(i < j) {
	    char a = str.charAt(i), b = str.charAt(j);
	    if(a != b) return false;
	    i++;j--;
	    }
	    return true;
	    }
	    String next() {
	    while (st == null || !st.hasMoreElements()) {
	    try{st = new StringTokenizer(br.readLine());}
	    catch (IOException  e) {e.printStackTrace();}
	    }
	    return st.nextToken();
	    }
	    int nextInt(){ return Integer.parseInt(next()); }
	    long nextLong() { return Long.parseLong(next());}
	    static int fact(int n) {
	    if(n == 1) return 1;
	    return n * fact(n-1);
	   	}
	    public int[] readIntArray(int n) {
	    int[] arr = new int[n];
		for(int i=0; i<n; ++i)
		arr[i]=nextInt();
		return arr;
		}
	    public int[][] readIntArray(int m, int n){
		int[][] arr = new int[m][n];
		for(int i = 0;i<m;i++)
		for(int j = 0;j<n;j++)
		arr[i][j] = nextInt();
		return arr;
	    }
	    public String[] readStringArray(int n) {
		String[] arr = new String[n];
		for(int i=0; i<n; ++i)
		arr[i]= nextLine();
		return arr;
	    }
		static int gcd(int a, int b) {
		if (b == 0) return a;
		return gcd(b, a % b);
		}
	    double nextDouble() {return Double.parseDouble(next());}
	    String nextLine() {
	    String str = "";
	    try{str = br.readLine();}
	    catch (IOException e) {e.printStackTrace();}
	    return str;}
	    }

		static int solve(int[] l, int[] r, int n) {
			int x = Integer.MAX_VALUE, y = Integer.MIN_VALUE, min = Integer.MAX_VALUE, max = -1;
			if(n == 1) {
				return 0;
			}
			for(int i = 1;i<n;i++) {
				min = Math.min(min, r[i-1]);
				max = Math.max(max, l[i-1]);
				if(l[i] > min) {
					x = Math.min(x, l[i]);
					y = Math.max(y, max);
				}
				else if(r[i] < max) {
					x = Math.min(x, r[i]);
					y = Math.max(y, max);
				}
			}
			return (Math.abs(y-x));
		}
		public static void main(String args[]) throws Exception {
	    FastReader sc = new FastReader();
	    long start = System.currentTimeMillis();
	    int t = sc.nextInt();

	    while(t--  > 0 ) {
	    	int n = sc.nextInt();
	    	int[] l = new int[n];
	    	int[] r = new int[n];
	    for(int i =0;i<n;i++) {
	    		l[i]= sc.nextInt();
	    		r[i] = sc.nextInt();
	    }
	    out.println(solve(l,r,n));
	    }
	        	flush();
	    long end = System.currentTimeMillis();
	    NumberFormat formatter = new DecimalFormat("#0.00000");
	    //System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
	    }
	}


import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;
import java.io.*;
		public final class Solution {
		static PrintWriter out = new PrintWriter(System.out);
		static void flush() {
	 	out.flush();
	 	}
		static class FastReader {
	    BufferedReader br;
	    StringTokenizer st;

	    public FastReader() {
	    br = new BufferedReader(new
	    InputStreamReader(System.in));
	    }
	    static boolean isPalindrome(String str1, String str2) {
	    String str3 = str1+str2;
	    int i = 0, j = str3.length()-1;
	    while(i < j) {
	    char a = str3.charAt(i), b = str3.charAt(j);
	    if(a != b) return false;
	    i++;j--;
	    }
	    return true;
	    }
	    static boolean isPalindrome(String str) {
	    int i = 0, j = str.length()-1;
	    while(i < j) {
	    char a = str.charAt(i), b = str.charAt(j);
	    if(a != b) return false;
	    i++;j--;
	    }
	    return true;
	    }
	    String next() {
	    while (st == null || !st.hasMoreElements()) {
	    try{st = new StringTokenizer(br.readLine());}
	    catch (IOException  e) {e.printStackTrace();}
	    }
	    return st.nextToken();
	    }
	    int nextInt(){ return Integer.parseInt(next()); }
	    long nextLong() { return Long.parseLong(next());}
	    static int fact(int n) {
	    if(n == 1) return 1;
	    return n * fact(n-1);
	   	}
	    public int[] readIntArray(int n) {
	    int[] arr = new int[n];
		for(int i=0; i<n; ++i)
		arr[i]=nextInt();
		return arr;
		}
	    public int[][] readIntArray(int m, int n){
		int[][] arr = new int[m][n];
		for(int i = 0;i<m;i++)
		for(int j = 0;j<n;j++)
		arr[i][j] = nextInt();
		return arr;
	    }
	    public String[] readStringArray(int n) {
		String[] arr = new String[n];
		for(int i=0; i<n; ++i)
		arr[i]= nextLine();
		return arr;
	    }
		static int gcd(int a, int b) {
		if (b == 0) return a;
		return gcd(b, a % b);
		}
	    double nextDouble() {return Double.parseDouble(next());}
	    String nextLine() {
	    String str = "";
	    try{str = br.readLine();}
	    catch (IOException e) {e.printStackTrace();}
	    return str;}
	    }

		static int solve(int[] l, int[] r, int n) {
			int x = Integer.MAX_VALUE, y = Integer.MIN_VALUE, min = Integer.MAX_VALUE, max = -1;
			if(n == 1) {
				return 0;
			}
			for(int i = 1;i<n;i++) {
				min = Math.min(min, r[i-1]);
				max = Math.max(max, l[i-1]);
				if(l[i] > min) {
					x = Math.min(x, min);
					y = Math.max(y, l[i]);
				}
				else if(r[i] < max) {
					x = Math.min(x, r[i]);
					y = Math.max(y, max);
				}
			}
			return (Math.abs(y-x));
		}
		public static void main(String args[]) throws Exception {
	    FastReader sc = new FastReader();
	    long start = System.currentTimeMillis();
	    int t = sc.nextInt();

	    while(t--  > 0 ) {
	    	int n = sc.nextInt();
	    	int[] l = new int[n];
	    	int[] r = new int[n];
	    for(int i =0;i<n;i++) {
	    		l[i]= sc.nextInt();
	    		r[i] = sc.nextInt();
	    }
	    out.println(solve(l,r,n));
	    }
	        	flush();
	    long end = System.currentTimeMillis();
	    NumberFormat formatter = new DecimalFormat("#0.00000");
	    //System.out.print("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
	    }
	}


import java.io.*;
import java.util.*;

public class Solver {
    static ArrayList<Integer> graph[];
    static boolean used[];
    static int mt[];
    static boolean prime[];

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int t = nextInt();
        for (int i = 0; i < t; i++) {
            int n = nextInt();
            int min = Integer.MAX_VALUE;
            int v = 0;
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < n; j++) {
                int a = nextInt();
                int b = nextInt();
                if (a < min){
                    min = a;
                    v = b;
                }
                if (max < a){
                    max = a;
                }
            }
            if (max - v < 0)pw.println(0);
            else pw.println(max - v);
        }
    }

    static StringTokenizer st = new StringTokenizer("");
    static BufferedReader br;

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.*;
import java.util.*;

public class Solver {
    static ArrayList<Integer> graph[];
    static boolean used[];
    static int mt[];
    static boolean prime[];

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int t = nextInt();
        for (int i = 0; i < t; i++) {
            int n = nextInt();
            int min = Integer.MAX_VALUE;
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < n; j++) {
                int a = nextInt();
                int b = nextInt();
                if (a < min){
                    min = a;
                }
                if (max < a){
                    max = b;
                }
            }
            if(max <= min)pw.println(0);
            else pw.println(Math.abs(min - max));
        }
        pw.close();
    }

    static StringTokenizer st = new StringTokenizer("");
    static BufferedReader br;

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.*;
import java.util.*;

public class Solver {
    static ArrayList<Integer> graph[];
    static boolean used[];
    static int mt[];
    static boolean prime[];

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int t = nextInt();
        for (int i = 0; i < t; i++) {
            int n = nextInt();
            int min = Integer.MAX_VALUE;
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < n; j++) {
                int a = nextInt();
                int b = nextInt();
                if (a > max){
                    max = a;
                }
                if (min > b){
                    min = b;
                }
            }
            if(max <= min)pw.println(0);
            else pw.println(Math.abs(max - min));
        }
        pw.close();
    }

    static StringTokenizer st = new StringTokenizer("");
    static BufferedReader br;

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.*;
import java.util.*;

public class Solver {
    static ArrayList<Integer> graph[];
    static boolean used[];
    static int mt[];
    static boolean prime[];

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int t = nextInt();
        for (int i = 0; i < t; i++) {
            int n = nextInt();
            int min = Integer.MAX_VALUE;
            int v = 0;
            int max = Integer.MIN_VALUE;
            for (int j = 0; j < n; j++) {
                int a = nextInt();
                int b = nextInt();
                if (a < min){
                    min = a;
                    v = b;
                }
                if (max < a){
                    max = a;
                }
            }
            if (max - v < 0)pw.println(0);
            else pw.println(max - v);
        }
        pw.close();
    }

    static StringTokenizer st = new StringTokenizer("");
    static BufferedReader br;

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }
}


import java.io.*;
import java.util.*;

public class Main {
    String fileName = "";

    public void solve() throws IOException {
        // code here
        int n = nextInt();
        int ansA = Integer.MIN_VALUE;
        int ansB = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int a = nextInt();
            int b = nextInt();
            ansA = Math.max(ansA, a);
            ansB = Math.min(ansB, b);
        }
        //System.out.println("v " + ansA + " " + ansB);
        if (ansA > ansB) {
            out.println(ansA - ansB);
        } else {
            out.println(0);
        }
    }

    public static void main(String[] args) throws IOException {
        Locale.setDefault(Locale.US);
        new Main().run();
    }

    private void run() {
        try {
            if (fileName.equals("")) {
                br = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                br = new BufferedReader(new FileReader(fileName + ".in"));
                out = new PrintWriter(fileName + ".out");
            }

            int t = nextInt();

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

            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    BufferedReader br;
    StringTokenizer in;
    PrintWriter out;

    public String nextToken() throws IOException {
        while (in == null || !in.hasMoreTokens()) {
            in = new StringTokenizer(br.readLine());
        }
        return in.nextToken();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{

	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	int t=Integer.parseInt(bf.readLine());
	while(t-->0)
	{
	    int n=Integer.parseInt(bf.readLine());
	    int cmin=Integer.MAX_VALUE;
	  //  int c=0;
	    int dmax=Integer.MIN_VALUE;
	  //  int d=0;
	    for(int i=0;i<n;i++)
	    {
	        String s[]=bf.readLine().split(" ");
	        int a=Integer.parseInt(s[0]);
	        int b=Integer.parseInt(s[1]);
	        if(b<cmin)
	        {
	            cmin=b;
	           // c=b;
	        }
	        if(a>dmax)
	        {
	            dmax=a;
	           // d=b;
	        }

	    }
	   // System.out.println(dmax+" "+cmin);

	    System.out.println(Math.max((dmax-cmin),0));
	}

	}
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import sun.security.jgss.TokenTracker;

public class myFile
{
	public static void main(String args[])
	{
	   Scanner sc = new Scanner(System.in);
	   int query = sc.nextInt();
	   int start,end;
	   List<MyClass> classList  = new ArrayList<MyClass>();
	   while(query>0 && sc.hasNextInt())
	   {
		   start = 1123456789;
		   end = 0;
		   query--;
		   int n = sc.nextInt();
		   classList.clear();
		   for(int i=1;i<=n;i++)
		   {
			  int x1 = sc.nextInt();
			  int y1 = sc.nextInt();
			  MyClass obj = new MyClass(x1,y1);
			  start = Math.min(start, y1);
			  end = Math.max(end, x1);
			  classList.add(obj);
		   }
		   long ans = Math.abs(end-start);
		   int count=0;
		   for(int i=0;i<classList.size();i++)
		   {
			   int u = classList.get(i).getStart();
			   int v = classList.get(i).getEnd();

			   if(start>=u &&  end<=v)
				   count++;
		   }
		   if(count==classList.size())ans=0;
		   System.out.println(ans);
	   }

	}

}

class MyClass
{
	int start;
	int end;
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public MyClass(int start, int end) {
		super();
		this.start = start;
		this.end = end;
	}
	@Override
	public String toString() {
		return "MyClass [start=" + start + ", end=" + end + "]";
	}

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import sun.security.jgss.TokenTracker;

public class MyCode
{
	public static void main(String args[])
	{
	   Scanner sc = new Scanner(System.in);
	   int query = sc.nextInt();
	   int start,end;
	   List<MyClass> classList  = new ArrayList<MyClass>();
	   while(query>0 && sc.hasNextInt())
	   {
		   start = 1123456789;
		   end = 0;
		   query--;
		   int n = sc.nextInt();
		   classList.clear();
		   for(int i=1;i<=n;i++)
		   {
			  int x1 = sc.nextInt();
			  int y1 = sc.nextInt();
			  MyClass obj = new MyClass(x1,y1);
			  start = Math.min(start, y1);
			  end = Math.max(end, x1);
			  classList.add(obj);
		   }
		   long ans = Math.abs(end-start);
		   if(n==1)ans=0;
		   System.out.println(ans);
	   }

	}

}

class MyClass
{
	int start;
	int end;
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public MyClass(int start, int end) {
		super();
		this.start = start;
		this.end = end;
	}
	@Override
	public String toString() {
		return "MyClass [start=" + start + ", end=" + end + "]";
	}

}


package code_;

import java.io.*;
import java.util.*;

public class A_ {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            int n = in.nextInt();
            int minr = 2147483647;
            int maxl = -1;
            for (int j = 0; j < n; j++) {
                int l = in.nextInt();
                int r = in.nextInt();
                minr = Math.min(minr, r);
                maxl = Math.max(maxl, l);
            }
            out.println(Math.max(maxl - minr, 0));
        }
        out.close();
    }
}


//package code_;

import java.io.*;
import java.util.*;

public class A_ {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            int n = in.nextInt();
            int minr = 2147483647;
            int maxl = -1;
            for (int j = 0; j < n; j++) {
                int l = in.nextInt();
                int r = in.nextInt();
                minr = Math.min(minr, r);
                maxl = Math.max(maxl, l);
            }
            out.println(Math.max(maxl - minr, 0));
        }
        out.close();
    }
}


//package main;

import java.io.*;
import java.util.*;

public final class Main {
    BufferedReader br;
    StringTokenizer stk;

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    long mod = 1000_000_000 + 7;
    void run() throws Exception {
        int t = ni();
        while(t-- > 0) {
            int n = ni();
            List<Pair> pairs = new ArrayList<>(n);
            for(int i=0; i<n; i++) {
                pairs.add(new Pair(ni(), ni()));
            }
            int minr = Integer.MAX_VALUE;
            int maxl = Integer.MIN_VALUE;
            for(Pair p : pairs) {
                minr = Math.min(minr, p.right);
                maxl = Math.max(maxl, p.left );
            }
            System.out.println(Math.max(0, maxl - minr));
        }
    }

    class Pair {
        int left, right;
        public Pair(int left, int right) {
            this.left = left;
            this.right = right;
        }
        @Override
        public String toString() {
            return "[" + left + ", " + right + "]";
        }
    }

    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }

    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }
}


//package main;

import java.io.*;
import java.util.*;

public final class Main {
    BufferedReader br;
    StringTokenizer stk;

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    long mod = 1000_000_000 + 7;
    void run() throws Exception {
        int t = ni();
        while(t-- > 0) {
            int n = ni();
            List<Pair> pairs = new ArrayList<>(n);
            for(int i=0; i<n; i++) {
                pairs.add(new Pair(ni(), ni()));
            }
            Collections.sort(pairs, new Comparator<Pair>() {
                @Override
                public int compare(Pair p, Pair q) {
                    if(p.left == q.left)
                        return p.right - q.right;
                    return p.left - q.left;
                }
            });

            int L = pairs.get(n-1).left - pairs.get(0).right;
            if(L < 0) L = 0;

            System.out.println(L);
        }
    }

    class Pair {
        int left, right;
        public Pair(int left, int right) {
            this.left = left;
            this.right = right;
        }
    }

    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }

    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }
}


import java.util.Scanner;

public class A {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int j = 0;j<t;j++) {
            int n = in.nextInt();
            int l[] = new int[n];
            int r[] = new int[n];
            int minR = Integer.MAX_VALUE;
            int maxL = -1;
            for(int i = 0;i<n;i++) {
                l[i] = in.nextInt();
                if(l[i]>maxL) {
                    maxL = l[i];
                }
                r[i] = in.nextInt();
                if(r[i]<minR) {
                    minR = r[i];
                }
            }
            if(maxL>minR) {
                System.out.println(maxL-minR);
            } else {
                System.out.println(0);
            }
        }
    }
}


import java.io.*;
import java.util.*;

public class Main{
    public static void main(String[] args){
        InputReader in = new InputReader(System.in);
        int t = in.nextInt();
        while(t-- > 0){
            int n = in.nextInt();
            int min = Integer.MAX_VALUE;
            int max = Integer.MIN_VALUE;

            int l, r;
            if(n == 1){
                l = in.nextInt();
                r = in.nextInt();
                System.out.println(0);
                continue;
            }
            while(n-- > 0){
                l = in.nextInt();
                r = in.nextInt();
                if(l > max){
                    max = l;
                }
                if(r < min){
                    min = r;
                }
            }
            if(max - min > 0){
                System.out.println(max - min);
            }else{
                System.out.println(0);
            }
        }
    }

}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String nextLine()
    {
        String str = "";
        try
        {
            str = reader.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str;
    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author John Martin
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        AMathProblem solver = new AMathProblem();
        solver.solve(1, in, out);
        out.close();
    }

    static class AMathProblem {
        public void solve(int testNumber, InputReader c, OutputWriter w) {
            int tc = c.readInt();
            while (tc-- > 0) {
                int n = c.readInt();
                IntIntPair p[] = c.readIntPairArray(n);
                Arrays.sort(p);
                int l = p[0].second;
                for (int i = 0; i < n; i++) {
                    l = Math.min(l, p[i].second);
                }

                int r = p[n - 1].first;
                int res = Math.max(0, r - l);
                w.printLine(res);
            }
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public IntIntPair[] readIntPairArray(int size) {
            IntIntPair[] result = new IntIntPair[size];
            for (int i = 0; i < size; i++) {
                result[i] = readIntPair();
            }
            return result;
        }

        public IntIntPair readIntPair() {
            int first = readInt();
            int second = readInt();
            return new IntIntPair(first, second);
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class IntIntPair implements Comparable<IntIntPair> {
        public final int first;
        public final int second;

        public IntIntPair(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            IntIntPair pair = (IntIntPair) o;

            return first == pair.first && second == pair.second;
        }

        public int hashCode() {
            int result = first;
            result = 31 * result + second;
            return result;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        public int compareTo(IntIntPair o) {
            int value = Integer.compare(first, o.first);
            if (value != 0) {
                return value;
            }
            return Integer.compare(second, o.second);
        }

    }
}


/*      MOHD SADIQ        */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayList;

public class Main {

    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    private  long longVal() throws IOException{
        return Long.parseLong(br.readLine().trim());
    }
    private  int intVal() throws IOException{
        return Integer.parseInt(br.readLine().trim());
    }
    private  double doubleVal() throws IOException{
        return Double.parseDouble(br.readLine().trim());
    }
    private  int[] intArr() throws IOException{
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(s[i]);
        }
        return a;
    }
    private  long[] longArr() throws IOException {
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = Long.parseLong(s[i]);
        }
        return a;
    }
    private  double[] doubleArr() throws IOException {
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        double[] a = new double[n];
        for (int i = 0; i < n; i++) {
            a[i] = Double.parseDouble(s[i]);
        }
        return a;
    }
    private String[] stringArr() throws IOException{
        String[] s = br.readLine().trim().split(" ");
        return s;
    }
    private  char[] charArr() throws IOException{
        return br.readLine().trim().toCharArray();
    }

    private static String solve() throws IOException {

        Main obj = new Main();
        int n = obj.intVal();
        int[][] arr = new int[n][2];
        for(int i=0;i<n;i++)
            arr[i] = obj.intArr();
        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(Integer.compare(o1[0],o2[0])==0)
                    return Integer.compare(o1[1],o2[1]);
                return Integer.compare(o1[0],o2[0]);
            }
        });
//        for(int[] i:arr) {
//            for (int j : i) {
//                System.out.print(j + " ");
//            }
//            System.out.println();
//        }
        int ans = 0;
        int l = arr[0][0] ,r = arr[0][1];
        for(int i=1;i<n;i++){
//            System.out.println(l+" "+r);
            if(arr[i][0]>r){
//                System.out.println(l +" "+r);
                ans += arr[i][0]-r;
                l = arr[i][0];
                r = arr[i][0];
            }
            else {
                l = Math.max(l,arr[i][0]);
                r = Math.min(r,arr[i][1]);
            }
        }
        return String.valueOf(ans);
    }

    public static void main(String[] args) throws IOException {
        Main ob = new Main();
        int total_test_case = ob.intVal();
//        int total_test_case = 1;
        StringBuffer ans = new StringBuffer();
        String k ;
        for(int test_case =0;test_case<total_test_case;test_case++){
            k = solve();
//            System.out.println(k);
            ans.append(k+"\n");
        }
        System.out.print(ans);
    }
}


/*      MOHD SADIQ        */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayList;

public class Main {

    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    private  long longVal() throws IOException{
        return Long.parseLong(br.readLine().trim());
    }
    private  int intVal() throws IOException{
        return Integer.parseInt(br.readLine().trim());
    }
    private  double doubleVal() throws IOException{
        return Double.parseDouble(br.readLine().trim());
    }
    private  int[] intArr() throws IOException{
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(s[i]);
        }
        return a;
    }
    private  long[] longArr() throws IOException {
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = Long.parseLong(s[i]);
        }
        return a;
    }
    private  double[] doubleArr() throws IOException {
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        double[] a = new double[n];
        for (int i = 0; i < n; i++) {
            a[i] = Double.parseDouble(s[i]);
        }
        return a;
    }
    private String[] stringArr() throws IOException{
        String[] s = br.readLine().trim().split(" ");
        return s;
    }
    private  char[] charArr() throws IOException{
        return br.readLine().trim().toCharArray();
    }

    private static String solve() throws IOException {

        Main obj = new Main();
        int n = obj.intVal();
        int[] arr = obj.intArr();
        int min = arr[0],max = arr[1];
        int[] a  = new int[4];
        a[0] = arr[0];a[1] = arr[1];
        for(int i=0;i<n-1;i++){
            arr = obj.intArr();
            a[2] = arr[0];a[3] = arr[1];
            Arrays.sort(a);
            min = a[1];
            max = a[2];
        }
        if(n==1)
            return "0";
        return String.valueOf(Math.abs(min-max));
    }

    public static void main(String[] args) throws IOException {
        Main ob = new Main();
        int total_test_case = ob.intVal();
//        int total_test_case = 1;
        StringBuffer ans = new StringBuffer();
        String k ;
        for(int test_case =0;test_case<total_test_case;test_case++){
            k = solve();
//            System.out.println(k);
            ans.append(k+"\n");
        }
        System.out.print(ans);
    }
}


/*      MOHD SADIQ        */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayList;

public class Main {

    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    private  long longVal() throws IOException{
        return Long.parseLong(br.readLine().trim());
    }
    private  int intVal() throws IOException{
        return Integer.parseInt(br.readLine().trim());
    }
    private  double doubleVal() throws IOException{
        return Double.parseDouble(br.readLine().trim());
    }
    private  int[] intArr() throws IOException{
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(s[i]);
        }
        return a;
    }
    private  long[] longArr() throws IOException {
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = Long.parseLong(s[i]);
        }
        return a;
    }
    private  double[] doubleArr() throws IOException {
        String[] s = br.readLine().trim().split(" ");
        int n = s.length;
        double[] a = new double[n];
        for (int i = 0; i < n; i++) {
            a[i] = Double.parseDouble(s[i]);
        }
        return a;
    }
    private String[] stringArr() throws IOException{
        String[] s = br.readLine().trim().split(" ");
        return s;
    }
    private  char[] charArr() throws IOException{
        return br.readLine().trim().toCharArray();
    }

    private static String solve() throws IOException {

        Main obj = new Main();
        int n = obj.intVal();

        int[][] a = new int[n][2];
        int[] arr = obj.intArr();
        int min = arr[0],max = arr[1];
        for(int i=0;i<n-1;i++){
            arr = obj.intArr();
            min = Math.max(min,arr[0]);
            max = Math.min(max,arr[1]);
        }
        if(n==1)
            return "0";
        return String.valueOf(Math.abs(min-max));
    }

    public static void main(String[] args) throws IOException {
        Main ob = new Main();
        int total_test_case = ob.intVal();
//        int total_test_case = 1;
        StringBuffer ans = new StringBuffer();
        String k ;
        for(int test_case =0;test_case<total_test_case;test_case++){
            k = solve();
//            System.out.println(k);
            ans.append(k+"\n");
        }
        System.out.print(ans);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.StringTokenizer;

public class TaskA {
    public static String doMain(Reader reader) throws IOException {
        MyReader in = new MyReader(reader);
        int i = in.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i; j++) {
            sb.append(task(in));
            sb.append("\n");
        }
        return sb.toString();
    }

    private static String task(MyReader in) throws IOException {
        int n = in.nextInt();
        int minr = 1_000_000_001;
        int maxl = -1;
        for (int i = 0; i < n; i++) {
            int l = in.nextInt();
            int r = in.nextInt();
            minr = Math.min(minr, r);
            maxl = Math.max(maxl, l);
        }
        return "" + Math.max(0, maxl - minr);
    }

    public static void main(String[] args) throws IOException {
        String result = doMain(new InputStreamReader(System.in));
        System.out.println(result);
    }

    static class MyReader {
        BufferedReader bf;

        StringTokenizer st;

        String last;

        MyReader(Reader reader) throws IOException {
            bf = new BufferedReader(reader);
            readNextLine();
        }

        String nextToken() throws IOException {
            while (!st.hasMoreTokens()) {
                readNextLine();
            }
            return st.nextToken();
        }

        void readNextLine() throws IOException {
            last = bf.readLine();
            if (last == null) last = "";
            st = new StringTokenizer(last);
        }

        String nextLine() throws IOException {
            String s = last;
            readNextLine();
            return s;
        }

        long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

        int[] readIntArray(int n) throws IOException {
            int[] answer = new int[n];
            for (int i = 0; i < n; ++i) {
                answer[i] = nextInt();
            }
            return answer;
        }

        long[] readLongArray(int n) throws IOException {
            long[] answer = new long[n];
            for (int i = 0; i < n; ++i) {
                answer[i] = nextLong();
            }
            return answer;
        }

        double[] readDoubleArray(int n) throws IOException {
            double[] answer = new double[n];
            for (int i = 0; i < n; ++i) {
                answer[i] = nextDouble();
            }
            return answer;
        }
    }
}


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0){
            int n=s.nextInt();
            int a=Integer.MAX_VALUE;
            int b=0;
            for (int i=0;i<n;i++) {
                cnt = Math.max(a, s.nextInt());
                y = Math.min(b, s.nextInt());

            }
            System.out.println(Math.max(a-b,0));
        }
    }
}


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0){
            int n=s.nextInt();
            int y=Integer.MAX_VALUE;
            int cnt=0;
            for (int i=0;i<n;i++) {
                cnt = Math.max(cnt, s.nextInt());
                y = Math.min(y, s.nextInt());

            }
            System.out.println(Math.max(cnt-y,0));
        }
    }
}


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0){
            int n=s.nextInt();
            int y=Integer.MAX_VALUE;
            int cnt=0;
            for (int i=0;i<n;i++)
                cnt = Math.min(cnt, s.nextInt());
                 y=Math.max(y,s.nextInt());

        }
    }
}


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0){
            int n=s.nextInt();
            int a=Integer.MAX_VALUE;
            int b=0;
            for (int i=0;i<n;i++) {
                a = Math.max(a, s.nextInt());
                b = Math.min(b, s.nextInt());

            }
            System.out.println(Math.max(a-b,0));
        }
    }
}


import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class Exam {
	public static long mod = (long)Math.pow(10, 9)+7 ;
	public static double epsilon=0.00000000008854;//value of epsilon
	public static InputReader sc = new InputReader(System.in);
	public static PrintWriter pw = new PrintWriter(System.out);
	public static void d(int n,int s,int a[],String ans){
		if(a[0]==1){
			return;
		}
		if(n<=0){
			if(Math.sqrt(s)-(int)Math.sqrt(s)==0)
			{
				a[0]=1;
				pw.println(ans);
			}
			return;
		}
		for(int i=1;i<10;i++){
			d(n-1,s+i*i,a,ans+i);
		}
	}
	public static int gr(int s,int e,int a[][],int k,int dp[][]){
		int t=0;
		if(dp[s][k]!=0)
		return dp[s][k];
		if(s==e&&k==0){
			dp[s][k]=1;
			return 1;
		}
		if(k<=0){
			dp[s][k]=0;
			return 0;
		}
		for(int i=0;i<a[s].length;i++){
			if(a[s][i]==1){
				t+=gr(i,e,a,k-1,dp);
			}
		}
		dp[s][k]=t;
		return t;
	}
	public static int f(int n,int k,int v[],int dp[][]){

		if(n==0)
		return 0;
		if(n<0||k<=0)
		return -1;
		if(dp[n][k]!=0){
			return dp[n][k];
		}
		int c=f(n-k,k,v,dp);
		if(c==-1){
			dp[n][k]=f(n, k-1, v, dp);
		}
		else{
			dp[n][k]=Math.max(c+v[k-1],f(n, k-1, v, dp));
		}
		return dp[n][k];
	}
	public static int f1(int n,int v[],int dp[]){
		if(n==0)
		return v[0];
		else if(n==1)
		return Math.max(v[0],v[1]);

		if(dp[n]!=0)
		return dp[n];
		else{
			dp[n]=Math.max(f1(n-2,v,dp)+v[n], f1(n-1,v,dp));
			return dp[n];
		}

	}
	public static double dis(int x,int y,int x1[],int y1[],int x2[],int y2[],int x3[],int y3[]){
		double d1[]=new double [x1.length];
		double d2[]=new double [x2.length];
		Arrays.fill(d2,Long.MAX_VALUE);
		for(int i=0;i<x1.length;i++){
			d1[i]=Math.sqrt(Math.pow(x1[i]-x, 2)+Math.pow(y1[i]-y, 2));
		}
		for(int i=0;i<x2.length;i++){
			for(int j=0;j<x3.length;j++){
				d2[i]=Math.min(Math.sqrt(Math.pow(x2[i]-x3[j], 2)+Math.pow(y2[i]-y3[j], 2)), d2[i]);
			}
		}
		double min=Long.MAX_VALUE;
		for(int i=0;i<x1.length;i++){
			for(int j=0;j<x2.length;j++){
				min=Math.min(d1[i]+d2[j]+Math.sqrt(Math.pow(x1[i]-x2[j], 2)+Math.pow(y1[i]-y2[j], 2)), min);
			}
		}
		return min;
	}
	public static int check(int b,int c,int a[][],int f[]){
		int d=0,in=-1;
		for(int i=0;i<a.length;i++){
			if(i!=f[0]){
				if(b==a[i][0]||b==a[i][1]||b==a[i][2]){
					if(c==a[i][0]||c==a[i][1]||c==a[i][2]){
						in=i;
						f[0]=in;
						break;
					}
				}
			}
		}
		if(in!=-1){
			for(int i=0;i<3;i++){
				if(a[in][i]!=b&&a[in][i]!=c)
				d=a[in][i];
			}
		}
		return d;
	}
	public static long dp(int n,int k,int m,long dp[][],long a[]){
		if(k==0&&n>=-1)
		return 0;
		if(n-m==-1&&k==1){
			dp[n][k]=a[n];
		}
		else if(n-m<0){
			dp[n][k]=0;
		}
		else if(dp[n][k]!=-1)
		return dp[n][k];
		else{
			dp[n][k]=Math.max((long)a[n]+dp(n-m,k-1,m,dp,a), dp(n-1,k,m,dp,a));
		}
		return dp[n][k];
	}
	public static void main(String[] args) {
		// code starts..
		int q=sc.nextInt();
		while(q-->0){
			int n=sc.nextInt();
			int l=Integer.MAX_VALUE,r=0;
			for(int i=0;i<n;i++){
				r=Math.max(sc.nextInt(), r);
				l=Math.min(sc.nextInt(), l);
			}
			if(r-l<=0){
				pw.println(0);
			}
			else
			pw.println(Math.abs(r-l));
		}
		// Code ends...
		pw.flush();
		pw.close();
	}
	static class tripletL implements Comparable<tripletL> {
		Long x, y, z;

		tripletL(long x, long y, long z) {
			this.x = x;
			this.y = y;
			this.z = z;
		}

		public int compareTo(tripletL o) {
			int result = x.compareTo(o.x);
			if (result == 0)
				result = y.compareTo(o.y);
			if (result == 0)
				result = z.compareTo(o.z);

			return result;
		}

		public boolean equlas(Object o) {
			if (o instanceof tripletL) {
				tripletL p = (tripletL) o;
				return (x - p.x == 0) && (y - p.y ==0 ) && (z - p.z == 0);
			}
			return false;
		}

		public String toString() {
			return x + " " + y + " " + z;
		}

		public int hashCode() {
			return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode();
		}
	}
	public static String Doubleformate(double a){
		DecimalFormat f =new DecimalFormat("#.00");
		return f.format(a);
	}

	public static Comparator<Integer[]> column(int i){
		return
		new Comparator<Integer[]>() {
			@Override
			public int compare(Integer[] o1, Integer[] o2) {
					Integer quantityOne = o1[i];
				Integer quantityTwo = o2[i];
				return quantityOne.compareTo(quantityTwo);
			}
		};
	}
	public static Comparator<Integer[]> pair(){
		return
		new Comparator<Integer[]>() {
			@Override
			public int compare(Integer[] o1, Integer[] o2) {
				int result=o1[0].compareTo(o2[0]);
				if(result==0)
				result=o1[1].compareTo(o2[1]);
				return result;
			}
		};
	}
	public static Comparator<Integer[]> Triplet(){
		return
		new Comparator<Integer[]>() {
			@Override
			public int compare(Integer[] o1, Integer[] o2) {

					for(int i=0;i<3;i++){
						for(int j=i+1;j<3;j++){
							for(int k=0;k<3;k++){
								for(int p=k+1;p<3;p++){
									if((o1[i]==o2[k]&&o1[j]==o2[p])||(o1[j]==o2[k]&&o1[i]==o2[p])){

									}
								}
							}
						}
					}
					int result=o1[0].compareTo(o2[0]);
					if(result==0)
					result=o1[1].compareTo(o2[1]);
					if(result==0)
					result=o1[2].compareTo(o2[2]);
					return result;
			}
		};
	}

	public static String reverseString(String s){
		StringBuilder input1 = new StringBuilder();
        input1.append(s);
		input1 = input1.reverse();
		return input1.toString();
	}
	public static int[] scanArray(int n){
		int a[]=new int [n];
		for(int i=0;i<n;i++)
		a[i]=sc.nextInt();

		return a;
	}
	public static long[] scanLongArray(int n){
		long a[]=new long [n];
		for(int i=0;i<n;i++)
		a[i]=sc.nextLong();

		return a;
	}
	public static String [] scanStrings(int n){
		String a[]=new String [n];
		for(int i=0;i<n;i++)
		a[i]=sc.nextLine();

		return a;
	}
	static class InputReader {

		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int[] nextIntArray(int n) {
			int a[] = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextInt();
			}
			return a;
		}

		public String readString() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		private boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}

}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.TreeSet;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;

public class taskA {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        StringTokenizer st;
        st = new StringTokenizer(in.nextLine(), " ");
        int t = Integer.parseInt(st.nextToken());
        TreeSet<Integer> points = new TreeSet<>();
        TreeSet<Integer> start = new TreeSet<>();
        TreeSet<Integer> end = new TreeSet<>();
        for (int i = 0; i < t; i++) {
            points.clear();
            start.clear();
            end.clear();
            st = new StringTokenizer(in.nextLine(), " ");
            int n = Integer.parseInt(st.nextToken());
            for (int j = 0; j < n; j++) {
                st = new StringTokenizer(in.nextLine(), " ");
                int l = Integer.parseInt(st.nextToken());
                int r = Integer.parseInt(st.nextToken());
                start.add(l);
                end.add(r);
                if (!points.contains(l)) {
                    points.add(l);
                }
                if (!points.contains(r)) {
                    points.add(r);
                }
            }
            if (points.size() == 1) {
                pw.println(0);
            } else {
                int ans1 = -1;
                int ans2 = -1;
                for (int x : points) {
                    if (ans1 == -1 && end.contains(x)) {
                        ans1 = x;
                    }
                    while (start.contains(x)) {
                        start.remove(x);
                    }
                    if (start.size() == 0) {
                        if (ans1 != -1) {
                            ans2 = x;
                        }
                        break;
                    }

                }
                pw.println(ans2 - ans1);
            }
        }
        pw.close();
    }
}


import java.io.BufferedReader;
// import java.io.FileInputStream;
// import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.round;
import static java.util.Arrays.copyOf;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import static java.util.Collections.reverseOrder;
import static java.util.Collections.sort;

public class Solution {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        int n = in.nextInt();
        int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            max = max(max, in.nextInt());
            min = min(min, in.nextInt());
        }
        out.println(max(0, max - min));
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        for (int t = in.nextInt(); t-- > 0; )
            solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Solution().run();
    }
}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.TreeSet;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;

public class taskA {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        StringTokenizer st;
        st = new StringTokenizer(in.nextLine(), " ");
        int t = Integer.parseInt(st.nextToken());
        TreeSet<Integer> points = new TreeSet<>();
        TreeSet<Integer> start = new TreeSet<>();
        TreeSet<Integer> end = new TreeSet<>();
        for (int i = 0; i < t; i++) {
            points.clear();
            start.clear();
            end.clear();
            st = new StringTokenizer(in.nextLine(), " ");
            int n = Integer.parseInt(st.nextToken());
            for (int j = 0; j < n; j++) {
                st = new StringTokenizer(in.nextLine(), " ");
                int l = Integer.parseInt(st.nextToken());
                int r = Integer.parseInt(st.nextToken());
                start.add(l);
                end.add(r);
                if (!points.contains(l)) {
                    points.add(l);
                }
                if (!points.contains(r)) {
                    points.add(r);
                }
            }
            if (points.size() == 1) {
                pw.println(0);
            } else {
                Collections.sort(points);
                int ans1 = -1;
                int ans2 = -1;
                for (int x : points) {
                    if (ans1 == -1 && end.contains(x)) {
                        ans1 = x;
                    }
                    while (start.contains(x)) {
                        start.remove(x);
                    }
                    if (start.size() == 0) {
                        if (ans1 != -1) {
                            ans2 = x;
                        }
                        break;
                    }

                }
                pw.println(ans2 - ans1);
            }
        }
        pw.close();
    }
}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class taskA {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        StringTokenizer st;
        st = new StringTokenizer(in.nextLine(), " ");
        int t = Integer.parseInt(st.nextToken());
        ArrayList<Integer> points = new ArrayList<>();
        TreeSet<Integer> start = new TreeSet<>();
        TreeSet<Integer> end = new TreeSet<>();
        for (int i = 0; i < t; i++) {
            points.clear();
            start.clear();
            end.clear();
            st = new StringTokenizer(in.nextLine(), " ");
            int n = Integer.parseInt(st.nextToken());
            for (int j = 0; j < n; j++) {
                st = new StringTokenizer(in.nextLine(), " ");
                int l = Integer.parseInt(st.nextToken());
                int r = Integer.parseInt(st.nextToken());
                start.add(l);
                end.add(r);
                if (!points.contains(l)) {
                    points.add(l);
                }
                if (!points.contains(r)) {
                    points.add(r);
                }
            }
            if (points.size() == 1) {
                pw.println(0);
            } else {
                Collections.sort(points);
                int ans1 = -1;
                int ans2 = -1;
                for (int x : points) {
                    if (ans1 == -1 && end.contains(x)) {
                        ans1 = x;
                    }
                    while (start.contains(x)) {
                        start.remove(x);
                    }
                    if (start.size() == 0) {
                        if (ans1 != -1) {
                            ans2 = x;
                        }
                        break;
                    }

                }
                pw.println(ans2 - ans1);
            }
        }
        pw.close();
    }
}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;

public class taskA {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
        StringTokenizer st;
        st = new StringTokenizer(in.nextLine(), " ");
        int t = Integer.parseInt(st.nextToken());
        ArrayList<Integer> points = new ArrayList<>();
        ArrayList<Integer> start = new ArrayList<>();
        ArrayList<Integer> end = new ArrayList<>();
        for (int i = 0; i < t; i++) {
            points.clear();
            start.clear();
            end.clear();
            st = new StringTokenizer(in.nextLine(), " ");
            int n = Integer.parseInt(st.nextToken());
            for (int j = 0; j < n; j++) {
                st = new StringTokenizer(in.nextLine(), " ");
                int l = Integer.parseInt(st.nextToken());
                int r = Integer.parseInt(st.nextToken());
                start.add(l);
                end.add(r);
                if (!points.contains(l)) {
                    points.add(l);
                }
                if (!points.contains(r)) {
                    points.add(r);
                }
            }
            if (points.size() == 1) {
                pw.println(0);
            } else {
                Collections.sort(points);
                int count = 0;
                int ans1 = -1;
                int ans2 = -1;
                for (int x : points) {
                    if (ans1 == -1 && end.contains(x)) {
                        ans1 = x;
                    }
                    while (start.contains(x)) {
                        count++;
                        start.remove(start.indexOf(x));
                    }
                    if (count == n) {
                        if (ans1 != -1) {
                            ans2 = x;
                        }
                        break;
                    }

                }
                pw.println(ans2 - ans1);
            }
        }
        pw.close();
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author anand.oza
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AMathProblem solver = new AMathProblem();
        solver.solve(1, in, out);
        out.close();
    }

    static class AMathProblem {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int t = in.nextInt();
            for (int i = 0; i < t; i++) {
                solve(in, out);
            }
        }

        private void solve(InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] l = new int[n];
            int[] r = new int[n];
            for (int i = 0; i < n; i++) {
                l[i] = in.nextInt();
                r[i] = in.nextInt();
            }

            int answer = Util.max(l) - Util.min(r);
            answer = Math.max(0, answer);

            out.println(answer);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }

    static class Util {
        public static int max(int... x) {
            int max = Integer.MIN_VALUE;
            for (int i : x) {
                max = Math.max(i, max);
            }
            return max;
        }

        public static int min(int... x) {
            int min = Integer.MAX_VALUE;
            for (int i : x) {
                min = Math.min(i, min);
            }
            return min;
        }

    }
}


import java.util.Scanner;
import java.util.*;

public class CodeForcesFormat {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        while(t>0){
           int n = input.nextInt();
           int min = 0 ;
           int max = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++){
                  max = Math.max(max, input.nextInt());
                  min = Math.min(min, input.nextInt());
            }
            System.out.println(Math.max(max-min, 0));
            t--;
        }
    }
}


import java.util.Scanner;
import java.util.*;
import java.io.*;

public class CodeForcesFormat {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        while(t>0){
           int n = input.nextInt();
           int max = 0 ;
           int min = Integer.MAX_VALUE;
            for (int i = 0; i < n; i++){
                max = Math.max(max, input.nextInt());
                min = Math.min(min, input.nextInt());
            }
            System.out.println(Math.max(max-min, 0));
            t--;
        }
    }
}


import java.util.Scanner;

public class A1227 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for (int t=0; t<T; t++) {
            int N = in.nextInt();
            int minR = Integer.MAX_VALUE;
            int maxL = Integer.MIN_VALUE;
            for (int n=0; n<N; n++) {
                int L = in.nextInt();
                int R = in.nextInt();
                minR = Math.min(minR, R);
                maxL = Math.max(maxL, L);
            }
            int answer = Math.max(0, maxL-minR);
            System.out.println(answer);
        }
    }

}


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class Day9 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter writer = new PrintWriter(System.out);
        int t = Integer.parseInt(reader.readLine());
        for(int q = 0; q < t; ++q){
            int n = Integer.parseInt(reader.readLine());
            ArrayList<Integer> s = new ArrayList<>();
            ArrayList<Integer> f = new ArrayList<>();
            for(int i =0; i < n; ++i){
                StringTokenizer st = new StringTokenizer(reader.readLine());
                s.add(Integer.parseInt(st.nextToken()));
                f.add(Integer.parseInt(st.nextToken()));
            }
            Collections.sort(s);
            Collections.sort(f);
            writer.println(Math.max(0, s.get(s.size() - 1) - f.get(0)));
        }
        writer.close();
    }
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{

	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	int t=Integer.parseInt(bf.readLine());
	while(t-->0)
	{
	    int n=Integer.parseInt(bf.readLine());
	    int cmin=Integer.MAX_VALUE;
	  //  int c=0;
	    int dmax=Integer.MIN_VALUE;
	  //  int d=0;
	    for(int i=0;i<n;i++)
	    {
	        String s[]=bf.readLine().split(" ");
	        int a=Integer.parseInt(s[0]);
	        int b=Integer.parseInt(s[1]);
	        if(b<cmin)
	        {
	            cmin=b;
	           // c=b;
	        }
	        if(a>dmax)
	        {
	            dmax=a;
	           // d=b;
	        }

	    }
	   // System.out.println(dmax+" "+cmin);

	    System.out.println(Math.max((dmax-cmin),0));
	}

	}
}


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{

	BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	int t=Integer.parseInt(bf.readLine());
	while(t-->0)
	{
	    int n=Integer.parseInt(bf.readLine());
	    int cmin=Integer.MAX_VALUE;
	    int c=0;
	    int dmax=Integer.MIN_VALUE;
	    int d=0;
	    for(int i=0;i<n;i++)
	    {
	        String s[]=bf.readLine().split(" ");
	        int a=Integer.parseInt(s[0]);
	        int b=Integer.parseInt(s[1]);
	        if(a<cmin)
	        {
	            cmin=a;
	            c=b;
	        }
	        if(a>dmax)
	        {
	            dmax=a;
	            d=b;
	        }

	    }

	    System.out.println(Math.abs(c-d));
	}

	}
}


import java.util.*;

public class CodeForces1227A{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int t = input.nextInt();
		for(int i = 0;i<t;i++){
			int n = input.nextInt();
			int x = 0;
			int y = Integer.MAX_VALUE;
			for(int j = 0;j<n;j++){
				x = Math.max(x,input.nextInt());
				y = Math.min(y,input.nextInt());
			}
			System.out.println(Math.max(x-y,0));
		}
	}
}