#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto x : v) os << x << " ";
  return os << "\n";
}
template <class U, class V>
ostream &operator<<(ostream &os, const pair<U, V> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int> > seg(n);
    for (int i = 0; i < (n); ++i) cin >> seg[i].first >> seg[i].second;
    int a = INT_MAX, b = 0;
    for (int i = 0; i < (n); ++i) {
      a = min(a, seg[i].second);
      b = max(b, seg[i].first);
    }
    cout << max(0, b - a) << "\n";
  }
}
