#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long x = 0, y = 1000000007;
    while (n--) {
      long long l, r;
      cin >> l >> r;
      x = max(l, x);
      y = min(r, y);
    }
    long long res = max(x - y, 0ll);
    cout << res << endl;
  }
}
