#include <bits/stdc++.h>
using namespace std;
long long MAX(long long x, long long y) {
  if (x > y)
    return x;
  else
    return y;
}
long long MIN(long long x, long long y) {
  if (x < y)
    return x;
  else
    return y;
}
int main() {
  long long t, n, x, y, l, r;
  cin >> t;
  for (long long i = 0; i < t; i++) {
    cin >> n;
    x = 0;
    y = 1000000000;
    for (long long j = 0; j < n; j++) {
      cin >> l >> r;
      x = MAX(x, l);
      y = MIN(y, r);
    }
    cout << MAX((x - y), 0) << endl;
  }
}
