#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#include<unordered_map>
#include<unordered_set>
#include<stdio.h>
#include<stdlib.h>
typedef long long ll;
#define pi 3.1415926535
#define mod 1000000007 //change when needed
using namespace std;
// using namespace __gnu_pbds;
template <typename T>
T InvMod(T a,T b,T &x,T &y)
{
if(a==0)
{
x=0;
y=1;
return b;
}
T x1,y1;
T g=InvMod(b%a,a,x1,y1);
x=y1-(b/a)*x1;
y=x1;
return g;
}
ll fact(ll n)
{
if(n==1)
return 1;
return (n%mod*fact(n-1)%mod)%mod;
}
void sieve(vector<int> &v)
{
bool arr[1000004];
memset(arr,true,sizeof arr);
// memset(arr,0,sizeof arr);
for(int i=2;i*i<=1000002;i++)
{
if(arr[i]==true)
{
v.push_back(i);
for(int j=i*i;j<=1000002;j+=i)
{
arr[j]=false;
}
}
}
}
bool cmp(pair<int,int> a,pair<int,int> b)
{
return a.first>b.first;
}
int main()
{
ios_base::sync_with_stdio();
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
pair<ll,ll> arr[n];
ll tmp=0;
for(int i=0;i<n;i++)
{
cin>>arr[i].first>>arr[i].second;
if(i>0)
{
tmp+=max(0ll,arr[i].first-arr[i-1].second);
}
}
tmp+=max(0ll,arr[n%n].first-arr[n-1].second);
ll ans=1e18;
for(int i=0;i<n;i++)
{
tmp-=max(0ll,arr[(n+i-1)%n].first-arr[(n+i-2)%n].second);
ans=min(ans,tmp+arr[(i+n-1)%n].first);
tmp+=max(0ll,arr[(n+i-1)%n].first-arr[(n+i-2)%n].second);
}
cout<<ans<<"\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCi8vICNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KLy8gI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CiNpbmNsdWRlPHVub3JkZXJlZF9tYX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ICAgICAgIH0KICAgIH0KfQpib29sIGNtcChwYWlyPGludCxpbnQ+IGEscGFpcjxpbnQsaW50PiBiKQp7CiAgcmV0dXJuIGEuZmlyc3Q+Yi5maXJzdDsKfQppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oKTsKICAgIGNpbi50aWUoTlVMTCk7CiAgICBpbnQgdDsKICAgIGNpbj4+dDsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgbGwgbjsKICAgICAgY2luPj5uOwogICAgICBwYWlyPGxsLGxsPiBhcnJbbl07CiAgICAgIGxsIHRtcD0wOwogICAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICB7CiAgICAgICAgY2luPj5hcnJbaV0uZmlyc3Q+PmFycltpXS5zZWNvbmQ7CiAgICAgICAgaWYoaT4wKQogICAgICAgIHsKICAgICAgICAgIHRtcCs9bWF4KDBsbCxhcnJbaV0uZmlyc3QtYXJyW2ktMV0uc2Vjb25kKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgdG1wKz1tYXgoMGxsLGFycltuJW5dLmZpcnN0LWFycltuLTFdLnNlY29uZCk7CiAgICAgIGxsIGFucz0xZTE4OwogICAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICB7CiAgICAgICAgdG1wLT1tYXgoMGxsLGFyclsobitpLTEpJW5dLmZpcnN0LWFyclsobitpLTIpJW5dLnNlY29uZCk7CiAgICAgICAgYW5zPW1pbihhbnMsdG1wK2FyclsoaStuLTEpJW5dLmZpcnN0KTsKICAgICAgICB0bXArPW1heCgwbGwsYXJyWyhuK2ktMSklbl0uZmlyc3QtYXJyWyhuK2ktMiklbl0uc2Vjb25kKTsKICAgICAgfQogICAgICBjb3V0PDxhbnM8PCJcbiI7CiAgICB9Cn0=