#include <bits/stdc++.h>
using namespace std;
int i = 0;
int main() {
  int t, n;
  cin >> t;
  for (int k = 0; k < t; k++) {
    int a = 1e9, b = 0, ans;
    cin >> n;
    for (int i = 0; i < n; i++) {
      int l, r;
      cin >> l >> r;
      if (r < a) {
        a = r;
      }
      if (l > b) {
        b = l;
      }
    }
    ans = b - a;
    if (ans < 0) {
      ans = 0;
    }
    cout << ans << endl;
  }
  return 0;
}
