#include <bits/stdc++.h>
using namespace std;
inline bool intersect(int l1, int r1, int l2, int r2) {
  return l2 <= r1 && l1 <= r2;
}
inline pair<int, int> getIntesection(int l1, int r1, int l2, int r2) {
  return {max(l1, l2), min(r1, r2)};
}
void solve() {
  int n;
  cin >> n;
  int left = 1;
  int right = 1000000000;
  while (n--) {
    int l, r;
    cin >> l >> r;
    right = min(right, r);
    left = max(left, l);
  }
  cout << max(0, left - right) << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
