#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    long long n;
    cin >> n;
    vector<long long> a[2];
    long long x, y;
    long long m_y = INT_MAX;
    for (long long i = 0; i < n; i++) {
      cin >> x >> y;
      a[0].push_back(x);
      a[1].push_back(y);
      m_y = min(m_y, y);
    }
    long long m_x = INT_MIN;
    for (long long i = 0; i < n; i++) {
      if (m_y < a[0][i]) {
        m_x = max(m_x, a[0][i]);
      }
    }
    if (m_x == INT_MIN) {
      cout << 0 << endl;
    } else
      cout << m_x - m_y << endl;
  }
}
