#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<int> ans;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    vector<pair<int, int> > vect(n);
    int ma_l = -1, mi_r = 2 * 1e9;
    for (int i = 0; i < n; i++) {
      cin >> vect[i].first >> vect[i].second;
      ma_l = max(vect[i].first, ma_l);
      mi_r = min(mi_r, vect[i].second);
    }
    if (mi_r >= ma_l) {
      ans.push_back(0);
      continue;
    }
    ans.push_back(ma_l - mi_r);
  }
  for (auto x : ans) cout << x << '\n';
  return 0;
}
