#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long i, j, k, n, cnt = 0, x1, x2, mn = INT_MAX, mx = -1;
    cin >> n;
    vector<pair<long long, long long>> v;
    for (long long i = 0; i < n; i++) {
      long long a, b;
      cin >> a >> b;
      v.push_back({a, b});
    }
    sort((v).begin(), (v).end());
    for (long long i = 0; i < n; i++) {
      mn = min(mn, v[i].second);
      mx = max(mx, v[i].first);
    }
    cout << max(0LL, mx - mn) << endl;
  }
  return 0;
}
