#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    if (n == 1) {
      int a, b;
      cin >> a >> b;
      cout << "0" << endl;
      continue;
    } else {
      int mx = INT_MIN, mn = INT_MAX;
      for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        mx = max(mx, a);
        mn = min(mn, b);
      }
      int val = (mx - mn);
      if (val < 0) {
        cout << "0" << endl;
      } else {
        cout << val << endl;
      }
    }
  }
  return 0;
}
