#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> v;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      v.push_back(make_pair(x, y));
    }
    sort(v.begin(), v.end());
    int rmin = 1e9 + 5;
    for (auto x : v) {
      if (x.second <= rmin) {
        rmin = x.second;
      }
    }
    cout << max(0, v[n - 1].first - rmin) << endl;
  }
  return 0;
}
