You are playing another computer game, and now you have to slay n monsters. These monsters are standing in a circle, numbered clockwise from 1 to n. Initially, the i-th monster has a_i health.

You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by 1 (deals 1 damage to it). Furthermore, when the health of some monster i becomes 0 or less than 0, it dies and explodes, dealing b_i damage to the next monster (monster i + 1, if i < n, or monster 1, if i = n). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on.

You have to calculate the minimum number of bullets you have to fire to kill all n monsters in the circle.

Input

The first line contains one integer T (1 ≤ T ≤ 150000) — the number of test cases.

Then the test cases follow, each test case begins with a line containing one integer n (2 ≤ n ≤ 300000) — the number of monsters. Then n lines follow, each containing two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^{12}) — the parameters of the i-th monster in the circle.

It is guaranteed that the total number of monsters in all test cases does not exceed 300000.

Output

For each test case, print one integer — the minimum number of bullets you have to fire to kill all of the monsters.

Example

Input


1
3
7 15
2 14
5 3


Output


6
#include <bits/stdc++.h>
using namespace std;
const int E4 = 1e4 + 2;
const int E5 = 1e5 + 2;
const int E6 = 1e6 + 2;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 8;
const double eps = 1e-12;
const double pi = 3.14159265359;
const long long INFL = 1ll * INF * INF;
void getTime(double sub = 0.0) {
  cout << "Time : " << fixed << clock() * 1.0 / CLOCKS_PER_SEC - sub << '\n';
}
double getClock() { return clock() * 1.0 / CLOCKS_PER_SEC; }
long long pw(long long a, long long n, long long K = MOD) {
  return (n ? (pw(((a % K) * (a % K)) % K, n / 2ll, K) *
               ((n & 1ll) ? (a % K) : 1ll)) %
                  K
            : 1ll);
}
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
long long lcm(long long a, long long b) { return (b / gcd(a, b)) * a; }
int fact(int n) { return n > 0 ? n * fact(n - 1) : 1; }
long long fact(long long n) { return n > 0 ? n * fact(n - 1) : 1; }
int choose(int k, int n) {
  if (k > n) return 0;
  k = (n - k > k ? n - k : k);
  if (k == 0) return 1;
  int R = n;
  for (int i = 2; i <= k; i++) {
    R = R * (n - i + 1) / i;
  }
  return R;
}
long long choose(long long k, long long n) {
  if (k > n) return 0;
  k = (n - k > k ? n - k : k);
  if (k == 0) return 1;
  int R = n;
  for (int i = 2; i <= k; i++) {
    R = R * (n - i + 1) / i;
  }
  return R;
}
int lowbit(int m) { return m & (-m); }
int hibit(int m) {
  for (int i = 30; i >= 0; i--)
    if ((1 << i) & m) return i;
}
long long hibit(long long m) {
  for (int i = 62; i >= 0; i--)
    if ((1ll << i) & m) return i;
}
int getbit(int x, int b) { return (x >> b) & 1; }
long long getbit(long long x, int b) { return (x >> b) & 1; }
int setbit(int x, int b) { return x | (1 << b); }
void _setbit(int &x, int b) { x = setbit(x, b); }
long long setbit(long long x, int b) { return x | (1ll << b); }
void _setbit(long long &x, int b) { x = setbit(x, b); }
int unsetbit(int x, int b) { return x & ((~0) - (1 << b)); }
void _unsetbit(int &x, int b) { x = unsetbit(x, b); }
int bitcnt(int x) {
  x = x - ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  return ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
long long bitcnt(long long x) {
  int p1 = (x >> 32) & ((1ll << 32) - 1);
  int p2 = x & ((1ll << 32) - 1);
  return bitcnt(p1) + bitcnt(p2);
}
string bin(int x, int len = -1) {
  if (len == -1) {
    len = hibit(x) + 1;
  }
  string res = "";
  for (int i = len - 1; i >= 0; i--) res.push_back(getbit(x, i) + '0');
  return res;
}
string bin(long long x, int len = -1) {
  if (len == -1) {
    len = hibit(x) + 1;
  }
  string res = "";
  for (int i = len - 1; i >= 0; i--) res.push_back(getbit(x, i) + '0');
  return res;
}
int zerocnt(int x) { return hibit(x) - bitcnt(x) + 1; }
int zerocnt(long long x) { return hibit(x) - bitcnt(x) + 1; }
struct IO {
  IO operator>>(int &n) const {
    n = 0;
    bool sign = 0;
    int a = getchar();
    while (a < '0' || a > '9') {
      if (a == '-') {
        sign = 1;
        a = getchar();
        break;
      }
      a = getchar();
    }
    while (a >= '0' && a <= '9') {
      n = (n << 1) + (n << 3) + a - '0';
      a = getchar();
    }
    if (sign) n = ~(n - 1);
    return *this;
  }
  IO operator>>(long long &n) {
    n = 0;
    bool sign = 0;
    char c = getchar();
    while (c < '0' || c > '9') {
      if (c == '-') sign = true;
      c = getchar();
    }
    while (c >= '0' && c <= '9') {
      n = n * 10 + c - 48;
      c = getchar();
    }
    if (sign) n = ~(n - 1);
    return *this;
  }
  IO operator>>(double &x) const {
    scanf("%lf", &x);
    return *this;
  }
  IO operator>>(char &x) const {
    do {
      x = getchar();
    } while (x == ' ' || x == '\n');
    return *this;
  }
  IO operator>>(string &s) const {
    cin >> s;
    return *this;
  }
  IO operator<<(const int x) const {
    printf("%d", x);
    return *this;
  }
  IO operator<<(const double x) const {
    printf("%lf", x);
    return *this;
  }
  IO operator<<(const char x) const {
    putchar(x);
    return *this;
  }
  IO operator<<(const long long x) const {
    cout << x;
    return *this;
  }
  IO operator<<(string x) {
    printf("%s", (x.c_str()));
    return *this;
  }
} in, out;
namespace Input {
template <template <typename, typename...> class Container, typename T>
void cinv(Container<T> &a, int n) {
  for (int i = 0; i < n; i++) {
    T t;
    cin >> t;
    a.push_back(t);
  }
}
template <typename T>
void cinv(T *ptr, int len) {
  for (int i = 0; i < len; i++) {
    in >> *(ptr + i);
  }
}
template <typename T>
void cinv(T A, int n, int m) {
  for (int i = 0; i < n; i++) {
    cinv(A[i], m);
  }
}
}  // namespace Input
namespace Output {
void split(char A) {
  if (A != '0') {
    putchar(A);
  }
}
template <typename T>
void Print(T A, char sp = '0') {
  out << A;
  split(sp);
}
template <typename T1, typename T2>
void Print(pair<T1, T2> A, char sp = '0') {
  cout << '(' << A.first << ',' << A.second << ')';
  split(sp);
}
template <typename T>
void Print(T A[], int len, char sp = ' ') {
  for (int i = 0; i < len; i++) {
    out << A[i];
    split(sp);
  }
}
template <typename T>
void Print(T *A, int n, int m, char nsp = ' ', char sp = '\n') {
  for (int i = 0; i < n; i++) {
    Print(A[i], m, nsp);
    split(sp);
  }
}
template <template <typename, typename...> class Container, typename T>
void Print(Container<T> C, char sp = ' ') {
  for (auto i : C) {
    Print(i);
    split(sp);
  }
}
template <typename T>
void Print(stack<T> A, char sp = ' ') {
  while (A.size()) {
    Print(A.top());
    split(sp);
    A.pop();
  }
}
template <template <typename, typename...> class Container, typename T1,
          typename T2>
void Print(Container<T1, T2> C, char sp = ' ') {
  for (auto i : C) {
    Print(i);
    split(sp);
  }
}
template <typename T>
void Printl(T A) {
  Print(A, '\n');
}
}  // namespace Output
using namespace Output;
using namespace Input;
int t;
const int N = E6;
long long a[N], b[N];
int main() {
  cin >> t;
  while (t--) {
    int n;
    long long mx = -INFL, sum = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
      in >> a[i] >> b[i];
    }
    for (int i = 0; i < n; i++) {
      sum += (a[i] - b[(i - 1 + n) % n] > 0 ? a[i] - b[(i - 1 + n) % n] : 0);
      mx = (mx > (a[i] - b[(i - 1 + n) % n] > 0 ? a[i] - b[(i - 1 + n) % n]
                                                : 0) -
                        a[i]
                ? mx
                : (a[i] - b[(i - 1 + n) % n] > 0 ? a[i] - b[(i - 1 + n) % n]
                                                 : 0) -
                      a[i]);
    }
    out << sum - mx << '\n';
  }
}
