#include <bits/stdc++.h>
const int mod = 1e9 + 7;
const int N = 1e6 + 7;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<pair<long long int, long long int>> v(n);
    long long int mini = 1e10, maxi = 0;
    for (long long int i = 0; i < n; i++) {
      cin >> v[i].first >> v[i].second;
      if (v[i].second < mini) mini = v[i].second;
      if (v[i].first > maxi) maxi = v[i].first;
    }
    if (maxi <= mini)
      cout << "0\n";
    else
      cout << abs(maxi - mini) << endl;
  }
  return 0;
}
