#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<pair<long long int, long long int> > v;
    long long int x, y;
    for (long long int i = 0; i < n; i++) {
      cin >> x >> y;
      v.push_back(make_pair(x, y));
    }
    sort(v.begin(), v.end());
    x = v[0].second;
    y = v[n - 1].first;
    for (long long int i = 1; i < n; i++) {
      if (v[i].second < x) x = v[i].second;
    }
    if (y < x) y = x;
    cout << y - x << '\n';
  }
}
