#include <bits/stdc++.h>
using namespace std;
const int MAX_NUM = 1e9 + 33;
const double EPS = 1e-3;
const double PI = acos(-1);
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int r_st = -MAX_NUM, l_end = MAX_NUM;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      r_st = max(r_st, a);
      l_end = min(l_end, b);
    }
    cout << max(r_st - l_end, 0) << "\n";
  }
  return 0;
}
