#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    int n;
    cin >> n;
    int mn = 1e9 + 1, mx = 0;
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      mn = min(mn, b);
      mx = max(mx, a);
    }
    cout << max(0, mx - mn) << '\n';
  }
}
