#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  int b = 0;
  for (const auto& a : v) o << (b++ ? ", " : "") << a;
  return o;
}
template <typename T>
ostream& operator<<(ostream& o, const set<T>& v) {
  int b = 0;
  for (const auto& a : v) o << (b++ ? ", " : "") << a;
  return o;
}
template <typename T, typename U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << p.first << ' ' << p.second;
  return o;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int _ = 0; _ < t; _++) {
    int n;
    cin >> n;
    pair<int, int> a;
    cin >> a.first >> a.second;
    pair<int, pair<int, int>> curr({0, a});
    for (int i = 0; i < n - 1; i++) {
      pair<int, int> b;
      a = curr.second;
      cin >> b.first >> b.second;
      if (b.first > a.second) {
        curr.second.second = b.first;
        if (curr.first == 0) {
          curr.second.first = a.second;
        }
        curr.first += b.first - a.second;
      } else if (b.second < a.first) {
        curr.second.first = b.second;
        if (curr.first == 0) {
          curr.second.second = a.first;
        }
        curr.first += a.first - b.second;
      } else if (curr.first == 0) {
        if (b.second < a.second) {
          curr.second.second = b.second;
        }
        if (b.first > a.first) {
          curr.second.first = b.first;
        }
      }
    }
    cout << curr.first << endl;
  }
}
