#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 5, LOG = 20, INF = 1e9 + 1, SQ = 375, base = 100,
                    MOD = 1e9 + 7;
int l[N], r[N];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> l[i] >> r[i];
    }
    int l1 = INF, r1 = 0;
    for (int i = 0; i < n; i++) {
      l1 = min(r[i], l1);
      r1 = max(l[i], r1);
    }
    bool st = true;
    for (int i = 0; i < n; i++) {
      if (l1 < l[i] || l1 > r[i]) st = false;
    }
    if (st) l1 = r1;
    cout << abs(l1 - r1) << '\n';
  }
  return 0;
}
