#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long k;
    cin >> k;
    vector<vector<long long>> q(k);
    for (long long j = 0; j < k; j++) {
      q[j].resize(2);
      cin >> q[j][0] >> q[j][1];
    }
    long long low = 9999999999;
    for (long long j = 0; j < k; j++)
      if (q[j][1] < low) low = q[j][1];
    long long high = 0;
    for (long long j = 0; j < k; j++)
      if (q[j][0] > high) high = q[j][0];
    if (high - low < 0)
      cout << 0 << endl;
    else
      cout << high - low << endl;
  }
}
