#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<pair<bool, int> > a;
    int x, y;
    for (int i = 0; i < n; ++i) {
      cin >> x >> y;
      a.push_back({0, x});
      a.push_back({1, y});
    }
    sort((a).begin(), (a).end());
    int ind_b = 0, ind_e = 0;
    for (int i = 0; i < n * 2; ++i) {
      if (a[i].first == 1) {
        ind_b = a[i].second;
        break;
      }
    }
    for (int i = n * 2 - 1; i >= 0; --i) {
      if (a[i].first == 0) {
        ind_e = a[i].second;
        break;
      }
    }
    if (ind_b >= ind_e) {
      cout << 0;
    } else {
      cout << ind_e - ind_b;
    }
    cout << endl;
  }
  return 0;
}
