#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    int arr1[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i] >> arr1[i];
    }
    sort(arr, arr + n);
    sort(arr1, arr1 + n);
    int x = arr[n - 1] - arr1[0];
    if (x > 0)
      cout << arr[n - 1] - arr1[0] << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}
