#include <bits/stdc++.h>
using namespace std;
bool isinter(pair<long long int, long long int> a,
             pair<long long int, long long int> b) {
  if (a.second < b.first || a.first > b.second) return 0;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long int t, n, i, a, b;
  cin >> t;
  while (t--) {
    vector<pair<long long int, long long int>> notnice;
    vector<pair<long long int, long long int>> all;
    cin >> n;
    for (i = 0; i < n; ++i) {
      cin >> a >> b;
      all.push_back(make_pair(a, b));
    }
    vector<pair<long long int, long long int>>::iterator itr;
    for (auto &it : all) {
      for (itr = notnice.begin(); itr != notnice.end(); ++itr) {
        if (isinter(*itr, it)) {
          a = (*itr).first;
          b = (*itr).second;
          (*itr).first = max(a, it.first);
          (*itr).second = min(b, it.second);
          break;
        }
      }
      if (itr == notnice.end()) notnice.push_back(it);
    }
    a = (*notnice.begin()).first;
    b = (*notnice.begin()).second;
    if (notnice.size() == 1) {
      cout << 0 << "\n";
      continue;
    }
    for (auto &it : notnice) {
      if (it.second < b) b = it.second;
      if (it.first > a) a = it.first;
    }
    cout << a - b << "\n";
  }
}
