#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v1(n), v2(n);
    for (long long i = 0; i < n; i++) {
      cin >> v1[i];
      cin >> v2[i];
    }
    sort(v1.begin(), v1.end());
    long long pp = v1[n - 1];
    sort(v2.begin(), v2.end());
    long long qq = v2[0];
    if (pp > qq)
      cout << pp - qq;
    else
      cout << 0;
    cout << "\n";
  }
  return 0;
}
