#include<bits/stdc++.h>
using namespace std ;
#define int long long
#define endl '\n'
const int maxn = 3e6 + 10 ;
const int mod = 1e9 + 7 ;
int a[maxn], b[maxn] ,c[maxn];
void solve() {
int n ; cin >> n ;
int tot = 0 ;
for(int i =1;i<=n;i++) {
cin >> a[i] >> b[i] ;
if(i > 1) {
c[i] = max(0ll , a[i] - b[i - 1]) ;
tot += c[i] ;
}
}
c[1] = max(0ll ,a[1] - b[n] ) ;
tot += c[1] ;
int mi = a[1] + tot - c[1];
for(int i =1;i<=n;i++) {
mi = min(mi , a[i] + tot - c[i]) ;
}
cout<<mi<<endl;
}
/*
*/
signed main () {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T = 1 ;
cin >> T ;
while(T--)solve() ;
return 0 ;
}
1234A - Equalize Prices Again | 1613A - Long Comparison |
1624B - Make AP | 660B - Seating On Bus |
405A - Gravity Flip | 499B - Lecture |
709A - Juicer | 1358C - Celex Update |
1466B - Last minute enhancements | 450B - Jzzhu and Sequences |
1582C - Grandma Capa Knits a Scarf | 492A - Vanya and Cubes |
217A - Ice Skating | 270A - Fancy Fence |
181A - Series of Crimes | 1638A - Reverse |
1654C - Alice and the Cake | 369A - Valera and Plates |
1626A - Equidistant Letters | 977D - Divide by three multiply by two |
1654B - Prefix Removals | 1654A - Maximum Cake Tastiness |
1649A - Game | 139A - Petr and Book |
1612A - Distance | 520A - Pangram |
124A - The number of positions | 1041A - Heist |
901A - Hashing Trees | 1283A - Minutes Before the New Year |