#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  pair<int, int> l, r;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (!i) {
      l = r = {x, y};
    }
    if (x > r.first) {
      r = {x, y};
    }
    if (y < l.second) {
      l = {x, y};
    }
  }
  int lft = l.second, rght = r.first;
  if (rght < lft) {
    rght = lft;
  }
  cout << rght - lft;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
    cout << '\n';
  }
  return 0;
}
