#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
const int MOD = 1e9 + 7;
long long power(long long x, long long y) {
  if (y == 0) return 1;
  long long s = power(x, y / 2);
  s *= s;
  if (y & 1) s *= x;
  return s;
}
long long Ceil(long long x, long long y) { return (x + y - 1) / y; }
bool check_intersections(vector<pair<int, int>>& a, int n) {
  int cnt = 0;
  for (int i = 0; i < a.size(); i++) {
    if (cnt == n) return 1;
    if (a[i].second == 1) cnt++;
    if (a[i].second == -1) return 0;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n), b;
    int minimum_end = 1e9, maximum_begin = -1;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      minimum_end = min(minimum_end, y);
      maximum_begin = max(maximum_begin, x);
      a[i] = {x, y};
      b.push_back({x, 1}), b.push_back({y, -1});
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    if (check_intersections(b, n)) {
      cout << "0\n";
      continue;
    }
    cout << (n == 1 ? 0 : abs(minimum_end - maximum_begin)) << "\n";
  }
  cin.get();
}
