#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, t, i, j;
  cin >> t;
  while (t--) {
    cin >> n;
    int mx = -1, mn = 2000000000, x1, x2;
    for (i = 1; i <= n; i++) {
      cin >> x1 >> x2;
      mx = max(x1, mx);
      mn = min(mn, x2);
    }
    cout << max(mx - mn, 0) << '\n';
  }
}
