#include <bits/stdc++.h>
using namespace std;
long long fact[1000002];
long long power(long long a, long long n) {
  long long r = 1;
  while (n > 0) {
    if (n & 1) {
      r = (r * a) % 1000000007;
    }
    a = a * a % 1000000007;
    n >>= 1;
  }
  return r;
}
long long ipow(long long a, long long b, long long c = 1000000007) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % 1000000007;
    a = a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
long long pre() {
  fact[0] = 1;
  for (long long i = 1LL; i < 1000002; i++) {
    fact[i] = fact[i - 1] * i;
    if (fact[i] >= 1000000007) fact[i] %= 1000000007;
  }
}
long long inverPow(long long num) {
  return ipow(num, 1000000007 - 2, 1000000007);
}
long long nCr(long long N, long long K) {
  long long ans = fact[N];
  long long temp = inverPow(fact[N - K]);
  ans = ans * temp % 1000000007;
  temp = inverPow(fact[K]);
  ans = ans * temp % 1000000007;
  return ans;
}
void solve() {
  long long n;
  cin >> n;
  long long x, y;
  long long left = 0;
  long long right = 1000000007;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    left = max(left, x);
    right = min(right, y);
  }
  cout << max(0ll, left - right) << "\n";
}
int main() {
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
