#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    int l = 0, r = INT_MAX;
    while (n--) {
      int x, y;
      cin >> x >> y;
      l = max(x, l);
      r = min(r, y);
    }
    cout << max(0, l - r) << "\n";
  }
  return 0;
}
