#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long> > v(n);
    for (long long i = 0; i < n; i++) {
      cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    long long a, b;
    a = v[n - 1].first;
    for (long long i = 0; i < n; i++) swap(v[i].first, v[i].second);
    sort(v.begin(), v.end());
    b = v[0].first;
    if (a - b <= 0)
      cout << 0 << endl;
    else
      cout << a - b << endl;
  }
  return 0;
}
