#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1000000007;
long long int MX = 2e9;
long long int INF = 1e17;
long long int t, n, x;
long long int hp[300300];
long long int dam[300300];
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> t;
  while (t--) {
    cin >> n;
    long long int res = 0;
    for (int i = 1; i <= n; i++) {
      cin >> hp[i] >> dam[i];
      res += hp[i];
    }
    for (int i = 1; i < n; i++) {
      dam[i] = min(dam[i], hp[i + 1]);
    }
    dam[n] = min(dam[n], hp[1]);
    sort(dam + 1, dam + n + 1);
    for (int i = 2; i <= n; i++) res -= dam[i];
    cout << res << endl;
  }
  return 0;
}
