#include <bits/stdc++.h>
using namespace std;
const int MAX = 500005;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, int>> a;
    int xm = 0, ym = INT_MAX;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      a.push_back(make_pair(x, y));
      if (x > xm) {
        xm = x;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (xm <= a[i].second) {
        continue;
      } else {
        ans = max(ans, xm - a[i].second);
      }
    }
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }
    cout << ans << endl;
  }
  return 0;
}
