#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void bboy() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long int find(double p) { return ceil(sqrt(2 * 365 * log(1 / (1 - p)))); }
long long int power(long long int x, unsigned long long int y,
                    long long int p) {
  long long int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int32_t main() {
  bboy();
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<pair<long long int, long long int> > v;
    for (long long int i = 0; i < n; ++i) {
      long long int l, r;
      cin >> l >> r;
      v.push_back({l, r});
    }
    long long int mn = v[0].second, mx = v[0].first, posmn = 0, posmx = 0;
    for (long long int i = 0; i < n; ++i) {
      if (v[i].second < mn) {
        mn = v[i].second;
        posmn = i;
      }
      if (v[i].first > mx) {
        mx = v[i].first;
        posmx = i;
      }
    }
    long long int l = v[posmn].second, r = v[posmx].first;
    if (r - l > 0)
      cout << r - l << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}
