#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    long long n;
    cin >> n;
    vector<pair<long long, long long>> vp;
    while (n--) {
      long long x, y;
      cin >> x >> y;
      vp.push_back(make_pair(x, y));
    }
    sort(vp.begin(), vp.end());
    long long one = vp[0].first, two = vp[0].second;
    long long f = 1;
    for (int i = 1; i < vp.size(); i++) {
      if (two >= vp[i].first) {
        one = vp[i].first;
        two = min(two, vp[i].second);
      } else {
        one = two;
        break;
      }
    }
    cout << abs(one - vp[vp.size() - 1].first) << endl;
  }
  return 0;
}
