#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll unsigned long long

using namespace std;

vector<ll> digits;

int main()
{
	ll k,n,i,j;
	cin>>k>>n;
	ll temp=n;
	ll rem;
	digits.clear();
	while(temp>0)
	{
		rem=temp%10;
		temp=temp/10;
		digits.push_back(rem);
	}
	ll sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
	ll ans=0;
//	for(i=0;i<digits.size();i++)
//		cout<<digits[i];
//	cout<<endl;
	sort(digits.begin(),digits.end());
	for(j=0;j<digits.size();j++)
			ans+=digits[j];
	for(i=0;i<digits.size();i++)
	{
		ans=ans+9-digits[i];
//		for(j=0;j<digits.size();j++)
//			cout<<digits[j];
		if(ans>=k)
		{
			cout<<i+1;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll long long

using namespace std;

int main()
{
	ll k,n,i,j;
	cin>>k>>n;
	ll len,temp=n;
	ll rem=0;
	vector<ll> digits;
	while(temp>0)
	{
		rem=temp%10;
		temp=temp/10;
		digits.push_back(rem);
	}
	ll sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
	ll ans=0;
	temp=10;
//	for(i=0;i<digits.size();i++)
//		cout<<digits[i];
//	cout<<endl;
	for(i=0;i<digits.size();i++)
	{
		temp=min_element(digits.begin(),digits.end())-digits.begin();
		digits[temp]=9;
		for(j=0;j<digits.size();j++)
			ans+=digits[j];
//		for(j=0;j<digits.size();j++)
//			cout<<digits[j];
		if(ans>=k)
		{
			cout<<i+1;
			return 0;
		}
		ans=0;
	}
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll unsigned long long

using namespace std;

int main()
{
	ll k,n,i,j;
	cin>>k>>n;
	ll len,temp=n;
	ll rem=0;
	vector<ll> digits;
	while(temp>0)
	{
		rem=temp%10;
		temp=temp/10;
		digits.push_back(rem);
	}
	ll sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
	ll ans=0;
	temp=10;
//	for(i=0;i<digits.size();i++)
//		cout<<digits[i];
//	cout<<endl;
	for(i=0;i<digits.size();i++)
	{
		temp=min_element(digits.begin(),digits.end())-digits.begin();
		digits[temp]=9;
		for(j=0;j<digits.size();j++)
			ans+=digits[j];
//		for(j=0;j<digits.size();j++)
//			cout<<digits[j];
		if(ans>=k)
		{
			cout<<i+1;
			return 0;
		}
		ans=0;
	}
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll unsigned long long

using namespace std;

int main()
{
	ll k,n,i,j;
	cin>>k>>n;
	ll len,temp=n;
	ll rem=0;
	vector<ll> digits;
	while(temp>0)
	{
		rem=temp%10;
		temp=temp/10;
		digits.push_back(rem);
	}
	ll sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
	ll ans=0;
	temp=10;
//	for(i=0;i<digits.size();i++)
//		cout<<digits[i];
//	cout<<endl;
	sort(digits.begin(),digits.end());
	for(j=0;j<digits.size();j++)
			ans+=digits[j];
	for(i=0;i<digits.size();i++)
	{
		ans=ans+9-digits[i];
//		for(j=0;j<digits.size();j++)
//			cout<<digits[j];
		if(ans>=k)
		{
			cout<<i+1;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll long long

using namespace std;

int main()
{
	ll k,n,i,j;
	cin>>k>>n;
	int len,temp=n;
	int cnt=0,rem=0;
	vector<int> digits;
	while(temp>0)
	{
		rem=temp%10;
		temp=temp/10;
		cnt++;
		digits.push_back(rem);
	}
	int sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
	temp=n;
	rem=0;
	while(temp++)
	{
		int temp2=temp;
		while(temp2>0)
		{
			rem+=temp2%10;
			temp2/=10;
		}
		if(rem>=k)
		{
			cout<<abs(temp-n);
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll long long

using namespace std;

int main()
{
	ll k,n,i,j;
	cin>>k>>n;
	ll len,temp=n;
	ll rem=0;
	vector<ll> digits;
	while(temp>0)
	{
		rem=temp%10;
		temp=temp/10;
		digits.push_back(rem);
	}
	ll sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
	ll ans=0;
	for(i=0;i<digits.size();i++)
	{
		digits[i]=9;
		for(j=0;j<digits.size();j++)
			ans+=digits[j];
		if(ans>=k)
		{
			cout<<i+1;
			return 0;
		}
		ans=0;
	}
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll long long

using namespace std;

int main()
{
	ll k,n,i,j;
	cin>>k>>n;
	ll len,temp=n;
	ll cnt=0,rem=0;
	vector<ll> digits;
	while(temp>0)
	{
		rem=temp%10;
		temp=temp/10;
		cnt++;
		digits.push_back(rem);
	}
	ll sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
	temp=n;
	rem=0;
	while(temp++)
	{
		ll temp2=temp;
		while(temp2>0)
		{
			rem+=temp2%10;
			temp2/=10;
		}
		if(rem>=k)
		{
			cout<<abs(temp-n);
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#include <vector>
#include <queue>

#define ll long long

using namespace std;

vector<ll> digits;

int main()
{
	ll k,i,j;
	string n;
	cin>>k>>n;
	digits.clear();
	i=0;
	while(i<n.size())
	{
		int rem=n[i]-'0';
		digits.push_back(rem);
		i++;
	}
	ll sum=0;
	for(i=0;i<digits.size();i++)
		sum+=digits[i];
	if(sum>=k)
	{
		cout<<0;
		return 0;
	}
//	for(i=0;i<digits.size();i++)
//		cout<<digits[i];
//	cout<<endl;
	sort(digits.begin(),digits.end());
	for(i=0;i<digits.size();i++)
	{
		sum=sum+9-digits[i];
//		for(j=0;j<digits.size();j++)
//			cout<<digits[j];
		if(sum>=k)
		{
			cout<<i+1;
			return 0;
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
main()
{ long  long n,k;
   cin>>k>>n;
long long  p=0,q=n,i=0;
  while(q>0)
  {  p=p+q%10;
     q=q/10;

  }
  //cout<<p<<endl;
  if(p>=k)
  cout<<"0"<<endl;
  else
  {  cout<<k-p<<endl;
  }
}


#include<iostream>
using namespace std;
main()
{  long k;
long long n,p=0,q=n,i=0;
   cin>>k>>n;

  while(q>0)
  {  p=p+q%10;
     q=q/10;

  }
 // cout<<i<<endl;
  if(p>=k)
  cout<<"0"<<endl;
  else
  {  cout<<k-p<<endl;
  }
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
main()
{ long  long n,k;
   vector<long>v;
   cin>>k>>n;
long long  p=0,q=n,i=0;
  while(q>0)
  { long c=q%10;
   p=p+c;
     q=q/10;
     v.push_back(c);

  }
  long x=p;
  //cout<<p<<endl;
  if(p>=k)
  cout<<"0"<<endl;
  else
  {  sort(v.begin(),v.end());
     long c=p,d=0,q;
      c=k-p;
      i=0;
      while(c>=0)
      { q=9-v[i];
        if(q<=c)
        { d++;
           c=c-q;
           if(c==0)
             break;
		}
		else
		{ d++;
		break;
		}i++;

  }
  cout<<d<<endl;
}
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
main()
{ long  long k;
  char n[10^100000];
   vector<long>v;
   cin>>k>>n;
long long  p=0,i=0,q=0;
long long l=strlen(n);

  while(i<l)
  {
   p=p+n[i]-48;
     int c=n[i]-48;
     v.push_back(c);
     i++;

  }
  cout<<"total="<<p<<endl;

  if(p>=k)
  cout<<"0"<<endl;
  else
  {  sort(v.begin(),v.end());
     long c=p,d=0,q;
      c=k-p;
      i=0;
      while(c>=0)
      { q=9-v[i];
        if(q<=c)
        { d++;
      //  cout<<c<<endl;
           c=c-q;

           if(c==0)
             break;
		}
		else
		{ d++;
		//cout<<d<<endl;
		break;
		}i++;

  }
  cout<<d<<endl;
}
}


#include<iostream>
using namespace std;
main()
{  long n,k;
   cin>>k>>n;
long p=0,q=n,i=0;
  while(q>0)
  {  p=p+q%10;
     q=q/10;
     i++;
  }
 // cout<<i<<endl;
  if(p>=k)
  cout<<"0"<<endl;
  else
  {  cout<<k-p<<endl;
  }
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
main()
{ long  long n,k;
   vector<long>v;
   cin>>k>>n;
long long  p=0,q=n,i=0;
  while(q>0)
  { long c=q%10;
   p=p+c;
     q=q/10;
     v.push_back(c);

  }
  long x=p;
  //cout<<p<<endl;
  if(p>=k)
  cout<<"0"<<endl;
  else
  {  sort(v.begin(),v.end());
     long c,d=0;
      c=k-p;
      i=0;
      while(c>=0)
      {  x=9-v[i];

          if(c<=x)
            {d++;
            break;
			}
            else
            { c=c-v[i];
            d++;
            i++;
			}
	  }
	  cout<<d<<endl;
  }
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
main()
{ long  long n,k;
   vector<long>v;
   cin>>k>>n;
long long  p=0,q=n,i=0;
  while(q>0)
  { long c=q%10;
   p=p+c;
     q=q/10;
     v.push_back(c);

  }
  long x=p;
  //cout<<p<<endl;
  if(p>=k)
  cout<<"0"<<endl;
  else
  {  sort(v.begin(),v.end());
     long c,d=0;
      c=k-p;
      i=0;
      while(c>=0)
      {  x=9-v[i];

          if(x>c||i==v.size())
            {
            break;
			}
            else
            { c=c-x;
            d++;
            i++;
			}
	  }
	  cout<<d<<endl;
  }
}


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
main()
{ long  long k;
  char n[10^100000];
   vector<long>v;
   cin>>k>>n;
long long  p=0,i=0,q=0;
long long l=strlen(n);

  while(i<l)
  {
   p=p+n[i]-48;
     int c=n[i]-48;
     v.push_back(c);
     i++;

  }
//  cout<<"total="<<p<<endl;

  if(p>=k)
  cout<<"0"<<endl;
  else
  {  sort(v.begin(),v.end());
     long c=p,d=0,q;
      c=k-p;
      i=0;
      while(c>=0)
      { q=9-v[i];
        if(q<=c)
        { d++;
      //  cout<<c<<endl;
           c=c-q;

           if(c==0)
             break;
		}
		else
		{ d++;
		//cout<<d<<endl;
		break;
		}i++;

  }
  cout<<d<<endl;
}
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
main()
{ long  long n,k;
   vector<long>v;
   cin>>k>>n;
long long  p=0,q=n,i=0;
  while(q>0)
  { long c=q%10;
   p=p+c;
     q=q/10;
     v.push_back(c);

  }
  long x=p;
  //cout<<p<<endl;
  if(p>=k)
  cout<<"0"<<endl;
  else
  {  sort(v.begin(),v.end());
     long c=p,d=0;

      i=0;
      while(c>=0)
      {  c=c+9-v[i];

          if(c>k)
            {d++;
            break;
			}
			else if(c==k){
			d++;
			break;}
            else
            {
            d++;
            i++;
			}
	  }
	  cout<<d<<endl;
  }
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int n;
string s;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
    }
//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
    else{
        int sub = n- su;
        int p = 0;
        int res = 0;
        for(int i = s.length()-1; i >= 0; i++){
            if(sub > 0){
                int temp = s[i] - '0';
                int tmp = 9 - temp;
                if(tmp < sub){
                    res += (tmp*pow(10,p));
                    sub -= (tmp*pow(10,p));
                }
                else{
                    res += (sub*pow(10,p));
                    sub -= (sub*pow(10,p));
                }
                p++;
            }
            else break;

        }
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int n;
string s;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
    }
//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
    else{
        int sub = n- su;
        int p = 0;
        int res = 0;
        for(int i = s.length()-1; i >= 0; i++){
            if(sub > 0){
                int temp = s[i] - '0';
                if(temp < sub){
                    res += (temp*pow(10,p));
                    sub -= (temp*pow(10,p));
                }
                else{
                    res += (sub*pow(10,p));
                    sub -= (sub*pow(10,p));
                }
                p++;
            }
            else break;

        }
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

typedef long long ll;

int n;
string s;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
    }
//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
    else{
        int sub = n- su;
        int p = 0;
        ll res = 0;
        for(int i = s.length()-1; i >= 0; i--){
            if(sub > 0){
                int temp = s[i] - '0';
                int tmp = 9 - temp;
//                cout << res << " " << tmp << endl;
                if(tmp < sub){
                    res++;
  //                  cout << pow(10,p) << endl;
                    sub -= (tmp);
                }
                else{
                    res++;
                    sub -= (sub);
                }
                p++;
    //            cout << res << endl;
            }
            else break;

        }
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

typedef long long ll;

int n;
string s;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
    }
//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
    else{
        int sub = n- su;
        int p = 0;
        ll res = 0;
        for(int i = s.length()-1; i >= 0; i++){
            if(sub > 0){
                int temp = s[i] - '0';
                int tmp = 9 - temp;
                if(tmp < sub){
                    res += (tmp*pow(10,p));
                    sub -= (tmp);
                }
                else{
                    res += (sub*pow(10,p));
                    sub -= (sub);
                }
                p++;
            }
            else break;

        }
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

int n;
string s;
vector<int> v;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
        v.push_back(temp);
    }
    sort(v.begin(),v.end());

//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
    else{
        int res = 0;
        int sub = n - su;
        for(int i = 0; i < (int) v.size() ;i++){
            if(sub > 0) {sub -= (9-v[i]);res++;}
            else break;
        }
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

typedef long long ll;

int n;
string s;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
    }
//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
    else{
        int sub = n- su;
        int p = 0;
        ll res = 0;
        for(int i = s.length()-1; i >= 0; i--){
            if(sub > 0){
                int temp = s[i] - '0';
                int tmp = 9 - temp;
//                cout << res << " " << tmp << endl;
                if(tmp < sub){
                    res += (tmp*pow(10,p));
  //                  cout << pow(10,p) << endl;
                    sub -= (tmp);
                }
                else{
                    res += (sub*pow(10,p));
                    sub -= (sub);
                }
                p++;
    //            cout << res << endl;
            }
            else break;

        }
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

int n;
string s;
vector<int> v;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
        v.push_back(temp);
    }sort(v.begin(),v.end());

//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
        else{
        int res = 0;
        int sub = n - su;
        for(int i = 0; i < (int) v.size() ;i++){
            if(sub > 0) {sub -= v[i];res++;}
            else break;
        }
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int n;
string s;

int main(){
    cin >> n >> s;
    int su = 0;
    for(int i = 0; i < s.length();i++){
        int temp = s[i] - '0';
        su += temp;
    }
//    cout << su << endl;
    if(su >= n ) cout << 0 << endl;
    else{
        int sub = n- su;
        int p = 0;
        int res = 0;
        for(int i = s.length()-1; i >= 0; i++){
            if(sub > 0){
                int temp = s[i] - '0';
                int tmp = 9 - temp;
                if(tmp < sub){
                    res += (tmp*pow(10,p));
                    sub -= (tmp);
                }
                else{
                    res += (sub*pow(10,p));
                    sub -= (sub);
                }
                p++;
            }
            else break;

        }
        cout << res << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long s,n,i=-1,c=0,sum=0,sm=0,dif,j;
	int d,a[1000000];
	memset(a,0,1000000*sizeof(int));
	cin>>s>>n;
	while(n>0)
	{
		d=n%10;
		a[++i]=9-d;
		sum=sum+d;
		n=n/10;
	}
	if(sum>=s)
	{
		cout<<"0"<<endl;
		return 0;
	}
	sort(a,a+(i+1));
	dif=s-sum;
	for(j=i;j>=0;j--)
	{
		sm=sm+a[j];c++;
		if(sm>=dif)
			break;
	}
	if(j>=0)
	cout<<c<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{	int c=0,cn=0,i,dif,d;
	long long sum,n1,n,sm=0,summ=0;
	string s,s1;
	cin>>sum;
	cin>>s;
	sort(s.begin(),s.end());
	for(i=0;i<s.length();i++)
	sm=sm+(s[i]-'0');
	if(sm>=sum)
	{
		cout<<"0"<<endl;
		return 0;
	}
	dif=sum-sm;
	for(i=0;i<s.length();i++)
	{
		summ=summ+(9-(s[i]-'0'));
		cn++;
		if(summ>=dif)
		break;

	}
	if(i<s.length())
	cout<<cn<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{	int c=0,cn=0,i,dif,d;
	long long sum,n1,n,sm=0,summ=0;
	string s,s1;
	cin>>sum;
	cin>>s;
	sort(s.begin(),s.end());
	for(i=0;i<s.length();i++)
	sm=sm+(s[i]-'0');
	if(sm>=sum)
	{
		cout<<"0"<<endl;
		return 0;
	}
	dif=sum-sm;
	for(i=0;i<s.length();i++)
	{
		summ=summ+(9-(s[i]-'0'));
		cn++;
		if(summ>=dif)
		break;

	}
	if(i<s.length())
	cout<<cn<<endl;
	else
	cout<<"-1"<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[1000000000];
int main()
{
	long long s,n,i=-1,c=0,sum=0,sm=0,dif,j;
	int d;
	cin>>s>>n;
	while(n>0)
	{
		d=n%10;
		a[++i]=9-d;
		sum=sum+d;
		n=n/10;
	}
	if(sum>=s)
	{
		cout<<"0"<<endl;
		return 0;
	}
	sort(a,a+(i+1));
	dif=s-sum;
	for(j=i;j>=0;j--)
	{
		sm=sm+a[j];c++;
		if(sm>=dif)
			break;
	}
	cout<<c<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{	int c=0,cn=0,i,dif,d;
	long long sum,n1,n,sm=0,summ=0;
	string s,s1;
	cin>>sum;
	cin>>s;
	sort(s.begin(),s.end());
	for(i=0;i<s.length();i++)
	sm=sm+(s[i]-'0');
	if(sm>=sum)
	{
		cout<<"0"<<endl;
		return 0;
	}
	dif=sum-sm;
	for(i=0;i<s.length();i++)
	{
		summ=summ+(9-(s1[i]-'0'));
		cn++;
		if(summ>=dif)
		break;

	}
	if(i<s.length())
	cout<<cn<<endl;
	else
	cout<<"-1"<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{	int c=0,cn=0,i,dif,d;
	long long sum,n1,n,sm=0,summ=0;
	string s,s1;
	cin>>sum;
	cin>>s;
	sort(s.begin(),s.end());
	s1=s;
	 n=atoi(s.c_str());
	n1=n;
	while(n1>0)
	{
		c++;
		d=n1%10;
		sm=sm+d;
		n1=n1/10;
	}
	if(sm>=sum)
	{
		cout<<"0"<<endl;
		return 0;
	}
	dif=sum-sm;
	for(i=0;i<=c;i++)
	{
		summ=summ+(9-(s1[i]-'0'));
		cn++;
		if(summ>=dif)
		break;

	}
	if(i<=c)
	cout<<cn<<endl;
	else
	cout<<"-1"<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long s,n,i=-1,c=0,sum=0,sm=0,dif,j;
	int d,a[1000000];
	memset(a,0,1000000*sizeof(int));
	cin>>s>>n;
	while(n>0)
	{
		d=n%10;
		a[++i]=9-d;
		sum=sum+d;
		n=n/10;
	}
	if(sum>=s)
	{
		cout<<"0"<<endl;
		return 0;
	}
	sort(a,a+(i+1));
	dif=s-sum;
	for(j=i;j>=0;j--)
	{
		sm=sm+a[j];c++;
		if(sm>=dif)
			break;
	}
	cout<<c<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<utility>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
 string str;
 long long k,sol;

int main()
{

cin>>k>>str;
for (int i=0;i<str.length();i++)sol+=(str[i]-'0');
if (sol>=k){cout<<0<<endl;return 0;}
else
{
 if (sol%9==0)cout<<sol/9<<endl;
 else cout<<sol/9+1<<endl;
}

return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<utility>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
 string str;
 long long k,sol,mapa[1000000+100];

int main()
{

cin>>k>>str;
for (int i=0;i<str.length();i++)sol+=(str[i]-'0'),mapa[str[i]-'0']++;
if (sol>=k){cout<<0<<endl;return 0;}

long long raz=k-sol;
sol=0;

for (int i=0;i<=9;i++)
{
     if (!raz){cout<<sol<<endl;return 0;}
      if (!mapa[i])continue;
    long long temp1=0;
    for (int j=1;j<=mapa[i];j++)
    {
            temp1+=9-i;
        if (temp1>raz){sol+=j;cout<<sol<<endl;return 0;}

    }
    sol+=mapa[i];
    raz-=(9-i)*mapa[i];
}

return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<utility>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
 string str;
 long long k,sol,mapa[1000000+100];

int main()
{

cin>>k>>str;
for (int i=0;i<str.length();i++)sol+=(str[i]-'0'),mapa[str[i]-'0']++;
if (sol>=k){cout<<0<<endl;return 0;}

int raz=k-sol;
sol=0;

for (int i=0;i<=9;i++)
{
    int temp1=9-i;
    sol++;
    raz-=min(raz,temp1);
    if (!raz){cout<<sol<<endl;return 0;}
}

return 0;
}


import java.util.*;
import java.lang.*;
public class Solution{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
      int t = input.nextInt();
      int n;
      double[] aList;
      double[] bList;
      double startIndex;
      double minDmg;
      for(int i = 0; i < t; i++){
        n = input.nextInt();
        minDmg = 300000;
        startIndex = 0;
        aList = new double[n];
        bList = new double[n];
        if(n > 1){
          aList[0] = input.nextDouble();
          bList[0] = input.nextDouble();
          for(int j = 1; j < n; j++){
            aList[j] = input.nextDouble();
            bList[j] = input.nextDouble();
            //System.out.printf("a: %d%nb: %d%n", (int) aList[j], (int) bList[j]);
            if(aList[j] < minDmg || bList[j - 1] < minDmg){
              minDmg = Math.min(aList[j], bList[j-1]);
              startIndex = j;
            }
          }
          if(aList[0] < minDmg || bList[n - 1] < 0){
            minDmg = Math.min(aList[0], bList[n-1]);
            startIndex = 0;
          }
        int turns = 0;
        for(int k = 0; k < n; k++){
          turns += aList[(int) (startIndex + k)%n];
          //System.out.printf("index: %d%nturns: %d%n", (int) (startIndex + k)%n, turns);
          //System.out.printf("a-b: %d%nmax: %d%n",(int) (aList[(int) (startIndex + k + 1)%n] - bList[(int) (startIndex + k)%n]), (int) (Math.max(aList[(int) (startIndex + k + 1)%n] - bList[(int) (startIndex + k)%n] , 0)) );
          aList[(int) (startIndex + k + 1)%n] = Math.max(aList[(int) (startIndex + k + 1)%n] - bList[(int) (startIndex + k)%n] , 0);
        }
        System.out.println(turns);
      }else{
        aList[0] = input.nextDouble();
        bList[0] = input.nextDouble();
        System.out.println(aList[0]);
      }
    }
  }
}


import java.util.*;
import java.lang.*;
public class Solution{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
      int t = input.nextInt();
      double maxTurns;
      double minDmg;
      for(int i = 0; i < t; i++){
        maxTurns = 0;
        int n = input.nextInt();
        double a1 = input.nextDouble();
        double bPrev;
        double aCur;
        minDmg = a1;
        for(int j = 1; j < n; j++){
          bPrev = input.nextDouble();
          aCur = input.nextDouble();
          maxTurns += Math.max(aCur - bPrev, 0);
          minDmg = Math.min(minDmg,aCur);
          minDmg = Math.min(minDmg,bPrev);
        }
        bPrev = input.nextDouble();
        maxTurns += Math.max(a1 - bPrev, 0);
        minDmg = Math.min(minDmg,a1);
        minDmg = Math.min(minDmg,bPrev);
        System.out.println((int) (maxTurns + minDmg));
      }
    }
  }


import java.util.*;
import java.lang.*;
public class Solution{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
      int t = input.nextInt();
      int n;
      int[] aList;
      int[] bList;
      int startIndex;
      int minDmg;
      for(int i = 0; i < t; i++){
        n = input.nextInt();
        minDmg = 300000;
        startIndex = 0;
        aList = new int[n];
        bList = new int[n];
        if(n > 1){
          aList[0] = input.nextInt();
          bList[0] = input.nextInt();
          for(int j = 1; j < n; j++){
            aList[j] = input.nextInt();
            bList[j] = input.nextInt();
            if(aList[j] < minDmg || bList[j - 1] < minDmg){
              minDmg = Math.min(aList[j], bList[j-1]);
              startIndex = j;
            }
          }
          if(aList[0] < minDmg || bList[n - 1] < 0){
            minDmg = Math.min(aList[0], bList[n-1]);
            startIndex = 0;
          }
        int turns = 0;
        for(int k = 0; k < n; k++){
          turns += aList[(startIndex + k)%n];
          aList[(startIndex + k + 1)%n] = Math.max(aList[(startIndex + k + 1)%n] - bList[(startIndex + k)%n] , 0);
        }
        System.out.println(turns);
      }else{
        aList[0] = input.nextInt();
        bList[0] = input.nextInt();
        System.out.println(aList[0]);
      }
    }
  }
}


import java.util.*;
import java.lang.*;
public class Solution{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
      int t = input.nextInt();
      double maxTurns;
      double minDmg;
      for(int i = 0; i < t; i++){
        maxTurns = 0;
        int n = input.nextInt();
        double a1 = input.nextDouble();
        minDmg = a1;
        for(int j = 1; j < n; j++){
          double bPrev = input.nextDouble();
          double aCur = input.nextDouble();
          maxTurns += Math.max(aCur - bPrev, 0);
          minDmg = Math.min(minDmg,aCur);
          minDmg = Math.min(minDmg,bPrev);
        }
        bPrev = input.nextDouble();
        maxTurns += Math.max(a1 - bPrev, 0);
        minDmg = Math.min(minDmg,a1);
        minDmg = Math.min(minDmg,bPrev);
        System.out.println((int) (maxTurns + minDmg));
      }
    }
  }


import java.util.*;
import java.lang.*;
public class Solution{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
      int t = input.nextInt();
      int n;
      double[] aList;
      double[] bList;
      double startIndex;
      double minDmg;
      for(int i = 0; i < t; i++){
        n = input.nextInt();
        minDmg = 300000;
        startIndex = 0;
        aList = new double[n];
        bList = new double[n];
        aList[0] = input.nextDouble();
        bList[0] = input.nextDouble();
        for(int j = 1; j < n; j++){
          aList[j] = input.nextDouble();
          bList[j] = input.nextDouble();
          if(aList[j] < minDmg || bList[j - 1] < minDmg){
            minDmg = Math.min(aList[j], bList[j-1]);
            startIndex = j;
          }
        }
        if(aList[0] < minDmg || bList[n - 1] < 0){
          minDmg = Math.min(aList[0], bList[n-1]);
          startIndex = 0;
        }
        int turns = 0;
        int index = 0;
        for(int k = 0; k < n; k++){
          index = (int) (startIndex + k)%n
          turns += aList[index];
          aList[(index + 1)%n] = Math.max(aList[index + 1)%n] - bList[index + 1)%n] , 0);
        }
        System.out.println(turns);
      }
    }
  }
}


import java.util.*;
import java.lang.*;
public class Solution{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
      int t = input.nextInt();
      int n;
      int[] aList;
      int[] bList;
      int startIndex;
      int minDmg;
      for(int i = 0; i < t; i++){
        n = input.nextInt();
        minDmg = 300000;
        startIndex = 0;
        aList = new int[n];
        bList = new int[n];
        aList[0] = input.nextInt();
        bList[0] = input.nextInt();
        for(int j = 1; j < n; j++){
          aList[j] = input.nextInt();
          bList[j] = input.nextInt();
          if(aList[j] < minDmg || bList[j - 1] < minDmg){
            minDmg = Math.min(aList[j], bList[j-1]);
            startIndex = j;
          }
        }
        if(aList[0] < minDmg || bList[n - 1] < 0){
          minDmg = Math.min(aList[0], bList[n-1]);
          startIndex = 0;
        }
      int turns = 0;
      for(int k = 0; k < n; k++){
        turns += aList[(startIndex + k)%n];
        aList[(startIndex + k + 1)%n] = Math.max(aList[(startIndex + k + 1)%n] - bList[(startIndex + k)%n] , 0);
      }
      System.out.println(turns);
    }
  }
}


import java.util.*;
import java.lang.*;
public class Solution{
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
      int t = input.nextInt();
      int n;
      double[] aList;
      double[] bList;
      double startIndex;
      double minDmg;
      for(int i = 0; i < t; i++){
        n = input.nextInt();
        minDmg = 300000;
        startIndex = 0;
        aList = new double[n];
        bList = new double[n];
        aList[0] = input.nextDouble();
        bList[0] = input.nextDouble();
        for(int j = 1; j < n; j++){
          aList[j] = input.nextDouble();
          bList[j] = input.nextDouble();
          System.out.printf("a: %d%nb: %d%n", (int) aList[j], (int) bList[j]);
          if(aList[j] < minDmg || bList[j - 1] < minDmg){
            minDmg = Math.min(aList[j], bList[j-1]);
            startIndex = j;
          }
        }
        if(aList[0] < minDmg || bList[n - 1] < 0){
          minDmg = Math.min(aList[0], bList[n-1]);
          startIndex = 0;
        }
        int turns = 0;
        int index = 0;
        for(int k = 0; k < n; k++){
          index = (int) (startIndex + k)%n;
          turns += aList[index];
          aList[(index + 1)%n] = Math.max(aList[(index + 1)%n] - bList[(index)%n] , 0);
        }
        System.out.println(turns);
      }
    }
  }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class A {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter printWriter = new PrintWriter(System.out);
        int t = Integer.parseInt(bufferedReader.readLine());
        for (int q = 0; q < t; q++) {
            int n = Integer.parseInt(bufferedReader.readLine());
            long[] a = new long[n];
            long[] b = new long[n];
            for (int i = 0; i < n; i++) {
                String s = bufferedReader.readLine();
                a[i] = Long.parseLong(s.split(" ")[0]);
                b[i] = Long.parseLong(s.split(" ")[1]);
            }
            long k = 0;
            long[] c = new long[n];
            for (int i = 0; i < n; i++) {
                if (i == 0) {
                    c[i] = a[i] - b[n - 1];
                } else {
                    c[i] = a[i] - b[i - 1];
                }
            }
            for (int i = 0; i < n; i++) {
                if (i != 0) {
                    if (c[i] > 0) {
                        k += c[i];
                    }
                } else {
                    k += a[i];
                }
            }
            long ans = k;
            for (int i = 1; i < n; i++) {
                if (c[i] > 0) {
                    k -= c[i];
                }
                k -= a[i - 1];
                k += a[i];
                if (c[i - 1] > 0) {
                    k += c[i - 1];
                }
                ans = Math.min(ans, k);
            }
            printWriter.println(ans);
            printWriter.close();
        }
    }
}


import java.util.*;

public class A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int q = 0; q < t; q++) {
            int n = scanner.nextInt();
            long[] a = new long[n];
            long[] b = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextLong();
                b[i] = scanner.nextLong();
            }
            long k = 0;
            for (int i = 0; i < n; i++) {
                if (i != 0) {
                    if (a[i] - b[i - 1] > 0) {
                        k += a[i] - b[i - 1];
                    }
                } else {
                    k += a[i];
                }
            }
            long ans = k;
            for (int i = 1; i < n; i++) {
                if (a[i] - b[i - 1] > 0) {
                    k -= a[i] - b[i - 1];
                }
                k -= a[i - 1];
                k += a[i];
                if (i != 1) {
                    if (a[i - 1] - b[i - 2] > 0) {
                        k += a[i - 1] - b[i - 2];
                    }
                } else {
                    if (a[i - 1] - b[n - 1] > 0) {
                        k += a[i - 1] - b[n - 1];
                    }
                }
                ans = Math.min(ans, k);
            }
            System.out.println(ans);
        }
    }
}


import java.util.*;

public class A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int q = 0; q < t; q++) {
            int n = scanner.nextInt();
            long[] a = new long[n];
            long[] b = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextLong();
                b[i] = scanner.nextLong();
            }
            long k = 0;
            long[] c = new long[n];
            for (int i = 0; i < n; i++) {
                if (i == 0) {
                    c[i] = a[i] - b[n - 1];
                } else {
                    c[i] = a[i] - b[i - 1];
                }
            }
            for (int i = 0; i < n; i++) {
                if (i != 0) {
                    if (c[i] > 0) {
                        k += c[i];
                    }
                } else {
                    k += a[i];
                }
            }
            long ans = k;
            for (int i = 1; i < n; i++) {
                if (c[i] > 0) {
                    k -= c[i];
                }
                k -= a[i - 1];
                k += a[i];
                if (c[i - 1] > 0) {
                    k += c[i - 1];
                }
                ans = Math.min(ans, k);
            }
            System.out.println(ans);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class A {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter printWriter = new PrintWriter(System.out);
        int t = Integer.parseInt(bufferedReader.readLine());
        for (int q = 0; q < t; q++) {
            int n = Integer.parseInt(bufferedReader.readLine());
            long[] a = new long[n];
            long[] b = new long[n];
            for (int i = 0; i < n; i++) {
                String s = bufferedReader.readLine();
                a[i] = Long.parseLong(s.split(" ")[0]);
                b[i] = Long.parseLong(s.split(" ")[1]);
            }
            long k = 0;
            long[] c = new long[n];
            for (int i = 0; i < n; i++) {
                if (i == 0) {
                    c[i] = a[i] - b[n - 1];
                } else {
                    c[i] = a[i] - b[i - 1];
                }
            }
            for (int i = 0; i < n; i++) {
                if (i != 0) {
                    if (c[i] > 0) {
                        k += c[i];
                    }
                } else {
                    k += a[i];
                }
            }
            long ans = k;
            for (int i = 1; i < n; i++) {
                if (c[i] > 0) {
                    k -= c[i];
                }
                k -= a[i - 1];
                k += a[i];
                if (c[i - 1] > 0) {
                    k += c[i - 1];
                }
                ans = Math.min(ans, k);
            }
            printWriter.println(ans);
        }
        printWriter.close();
    }
}


#include <bits/stdc++.h>
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define loop(i,n)        for(int i=0;i<n;i++)
#define ll               long long
#define N                998244353
using namespace std;
int main()
{
  CIN;
  int test;
  cin>>test;
  while(test--)
  {
    ll n;
    cin>>n;
    ll a[n], b[n], c[n];

    loop(i,n)
      cin>>a[i]>>b[i];

    ll ans=0;
    ll idx=INT_MAX, val=INT_MAX;
    loop(i, n)
    {
      ll tmp = max((ll)0, a[i]-b[(i-1+n)%n]);
      ans += tmp;
      if(tmp<=idx)
      {
        idx=tmp;
        val=min(a[i], val);
      }
    }

    cout<<ans+val<<endl;
  }

  return 0;
}


#include <bits/stdc++.h>
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define loop(i,n)        for(int i=0;i<n;i++)
#define ll               long long
#define N                998244353
using namespace std;
int main()
{
  CIN;
  ll test;
  scanf("%lld", &test);
  while(test--)
  {
    int n;
    scanf("%d", &n);
    ll a[n], b[n];

    loop(i,n)
      scanf("%lld %lld", &a[i], &b[i]);

    ll ans=0;
    ll val=1e18;

    loop(i, n)
    {
      ll tmp = max(0LL, a[i]-b[(i-1+n)%n]);
      ans += tmp;
      val = min(val, a[i]-tmp);
    }

    printf("%lld\n",ans+val);
  }

  return 0;
}


#include <bits/stdc++.h>
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define loop(i,n)        for(int i=0;i<n;i++)
#define ll               long long
#define N                998244353
using namespace std;
int main()
{
  CIN;
  ll test;
  scanf("%lld", &test);
  while(test--)
  {
    ll n;
    scanf("%lld", &n);
    ll a[n], b[n];

    loop(i,n)
      scanf("%lld %lld", &a[i], &b[i]);

    ll ans=0;
    ll val=INT_MAX;

    loop(i, n)
    {
      ll tmp = max(0LL, a[i]-b[(i-1+n)%n]);
      ans += tmp;
      val = min(val, a[i]-tmp);
    }

    cout<<ans+val<<endl;
  }

  return 0;
}


#include <bits/stdc++.h>
#define CIN              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define loop(i,n)        for(int i=0;i<n;i++)
#define ll               long long
#define N                998244353
using namespace std;
int main()
{
  CIN;
  int test;
  cin>>test;
  while(test--)
  {
    ll n;
    cin>>n;
    ll a[n], b[n], c[n];

    loop(i,n)
      cin>>a[i]>>b[i];

    ll ans=0;
    ll val=INT_MAX;
    loop(i, n)
    {
      ll tmp = max((ll)0, a[i]-b[(i-1+n)%n]);
      ans += tmp;
      val = min(val, a[i]-tmp);
    }

    cout<<ans+val<<endl;
  }

  return 0;
}


#include<iostream>
#include<vector>
#include<cstdio>

using namespace std;

int main(){
    long long int T;
    scanf("%lld",&T);
    long long int N,t,t_ans,ans;
    long long int s;
    long long int a,b;
    long long int A[300000],B[300000],C[300000];
    for ( int i = 0; i < T ; i++){
        scanf("%lld",&N);
        s=0;
        ans = 1e18;
        for(int i=0;i<N;i++){
            scanf("%lld %lld",&a,&b);
            A[i]=a;
            B[i]=b;
        }
        for(int i=0;i<N;i++){
            t = A[i]-B[(i-1+N)%N];
            t = t>0 ? t: 0;
            C[i]=t;
            s+=t;
        }
        for(int i=0;i<N;i++){
            t_ans = s+A[i]-C[i];
            ans = ans>t_ans ? t_ans:ans;
        }
        printf("%lld\n",ans);
    }
}


#include<iostream>
#include<vector>
#include<cstdio>

using namespace std;

void solve(){
    long long N,t,t_ans,ans=1e13;
    long long s=0;
    long long a,b;
    vector<long long> A,B,C;

    cin >> N;
    for ( int i=0; i<N;i++){
        cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        C.push_back(t);
        s +=t;
    }
    for (int i=0;i<N;i++){
        t_ans = s+A[i]-C[i];
        ans = ans>t_ans ? t_ans : ans;
    }
    cout << ans << endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for ( int i = 0; i < T ; i++){
        solve();
    }
}


#include<iostream>
#include<vector>

using namespace std;

void solve(){
    int N,t,ans;
    int s=0;
    int min_a=1e9;
    long long a,b;
    vector<long long> A,B;

    cin >> N;
    for ( int i=0; i<N;i++){
        cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
        min_a = min_a>a ? a : min_a;
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        s+=t;
    }
    cout <<s+min_a<< endl;
}

int main(){
    int T;
    cin >> T;
    for ( int i = 0; i < T ; i++){
        solve();
    }
}


#include<iostream>
#include<vector>

using namespace std;

void solve(){
    long long N,t,t_ans,ans=1e13;
    long long s=0;
    long long a,b;
    vector<long long> A,B,C;

    cin >> N;
    for ( int i=0; i<N;i++){
        cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        C.push_back(t);
        s+=t;
    }
    for (int i=0;i<N;i++){
        t_ans = s+A[i]-C[i];
        ans = ans>t_ans ? t_ans : ans;
    }
    cout << ans << endl;
}

int main(){
    int T;
    cin >> T;
    for ( int i = 0; i < T ; i++){
        solve();
    }
}


#include<iostream>
#include<vector>
#include<cstdio>

using namespace std;

void solve(){
    long long int N,t,t_ans,ans=1e13;
    long long int s=0;
    long long int a,b;
    vector<long long int> A,B,C;

    scanf("%lld",N);
    //cin >> N;
    for ( int i=0; i<N;i++){
        scanf("%lld %lld",&a,&b);
        //cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        C.push_back(t);
        s +=t;
    }
    for (int i=0;i<N;i++){
        t_ans = s+A[i]-C[i];
        ans = ans>t_ans ? t_ans : ans;
    }
    cout << ans << endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for ( int i = 0; i < T ; i++){
        solve();
    }
}


#include<iostream>
#include<vector>

using namespace std;

void solve(){
    int N,t,ans;
    int s=0;
    long long min_a=1e13;
    long long a,b;
    vector<long long> A,B;

    cin >> N;
    for ( int i=0; i<N;i++){
        cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
        min_a = min_a>a ? a : min_a;
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        s+=t;
    }
    cout <<s+min_a<< endl;
}

int main(){
    int T;
    cin >> T;
    for ( int i = 0; i < T ; i++){
        solve();
    }
}


#include<iostream>
#include<vector>
#include<cstdio>

using namespace std;

void solve(){
    long long int N,t,t_ans,ans=1e13;
    long long int s=0;
    long long int a,b;
    vector<long long int> A,B,C;

    scanf("%lld",&N);
    //cin >> N;
    for ( int i=0; i<N;i++){
        scanf("%lld %lld",&a,&b);
        //cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        C.push_back(t);
        s +=t;
    }
    for (int i=0;i<N;i++){
        t_ans = s+A[i]-C[i];
        ans = ans>t_ans ? t_ans : ans;
    }
    cout << ans << endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for ( int i = 0; i < T ; i++){
        solve();
    }
}


#include<iostream>
#include<vector>
#include<cstdio>

using namespace std;

void solve(){
    long long int N,t,t_ans,ans=1e13;
    long long int s=0;
    long long int a,b;
    vector<long long int> A,B,C;

    scanf("%lld",&N);
    //cin >> N;
    for ( int i=0; i<N;i++){
        scanf("%lld %lld",&a,&b);
        //cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        C.push_back(t);
        s +=t;
    }
    for (int i=0;i<N;i++){
        t_ans = s+A[i]-C[i];
        ans = ans>t_ans ? t_ans : ans;
    }
    printf("%lld\n",ans);
}

int main(){
    int T;
    cin >> T;
    long long int N,t,t_ans,ans;
    long long int s;
    long long int a,b;
    long long int A[300000],B[300000],C[300000];
    for ( int i = 0; i < T ; i++){
        scanf("%lld",&N);
        s=0;
        ans = 1e13;
        for(int i=0;i<N;i++){
            scanf("%lld %lld",&a,&b);
            A[i]=a;
            B[i]=b;
        }
        for(int i=0;i<N;i++){
            t = A[i]-B[(i-1+N)%N];
            t = t>0 ? t: 0;
            C[i]=t;
            s+=t;
        }
        for(int i=0;i<N;i++){
            t_ans = s+A[i]-C[i];
            ans = ans>t_ans ? t_ans:ans;
        }
        printf("%lld\n",ans);
    }
}


#include<iostream>
#include<vector>

using namespace std;

void solve(){
    long long N,t,ans;
    long long s=0;
    long long min_a=1e13;
    long long a,b;
    vector<long long> A,B;

    cin >> N;
    for ( int i=0; i<N;i++){
        cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
        min_a = min_a>a ? a : min_a;
    }
    for ( int i=0;i<N;i++){
        t = A[i]-B[(i-1+N)%N];
        t = t>0 ? t :0;
        s+=t;
    }
    cout <<s+min_a<< endl;
}

int main(){
    int T;
    cin >> T;
    for ( int i = 0; i < T ; i++){
        solve();
    }
}


#include<iostream>
#include<vector>
#include<cstdio>

using namespace std;

int main(){
    long long int T;
    scanf("%lld",&T);
    long long int N,t,t_ans,ans;
    long long int s;
    long long int a,b;
    long long int A[300000],B[300000],C[300000];
    for ( int i = 0; i < T ; i++){
        scanf("%lld",&N);
        s=0;
        ans = 1e15;
        for(int i=0;i<N;i++){
            scanf("%lld %lld",&a,&b);
            A[i]=a;
            B[i]=b;
        }
        for(int i=0;i<N;i++){
            t = A[i]-B[(i-1+N)%N];
            t = t>0 ? t: 0;
            C[i]=t;
            s+=t;
        }
        for(int i=0;i<N;i++){
            t_ans = s+A[i]-C[i];
            ans = ans>t_ans ? t_ans:ans;
        }
        printf("%lld\n",ans);
    }
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}
int C(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    int ans = 1;
    int i;

    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }

    return ans;
}
//Round off to nth precision
 void print_precision(long double x,int y){
     x*= (pow(10,y));
     x=round(x);
     x/=pow(10,y);
     cout<<fixed<<setprecision(y)<<x<<endl;
 }

void solve(){
   int n;cin>>n;
   vector<int>h(n);
   vector<int>d(n);
   priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q1;
   ff(n){cin>>h[i]>>d[i];q1.push(mp(h[i],i));}
   int res=0;

   while(!q1.empty()){

     int i = q1.top().second;q1.pop();
     if(h[i]==0){continue;}
     res+=h[i]; h[i]=0;

     int next = (i+1==n)?0:i+1;

     while(h[next]!=0){

         h[next]=max(0ll,h[next]-d[i]);

         if(h[next]!=0){
             q1.push(mp(h[next],next));
             break;
         }
         i=next; next=(i+1==n)?0:i+1;
     }

   }
   cout<<res<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}
int C(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    int ans = 1;
    int i;

    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }

    return ans;
}
//Round off to nth precision
 void print_precision(long double x,int y){
     x*= (pow(10,y));
     x=round(x);
     x/=pow(10,y);
     cout<<fixed<<setprecision(y)<<x<<endl;
 }

void solve(){
   int n;cin>>n;
   vector<int>h(n);
   vector<int>d(n);
   priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q1;
   ff(n){cin>>h[i]>>d[i];q1.push(mp(h[i],i));}
   int res=0; int d_max=-1; int r2=0;
   for(int i=0;i<n;i++){
       if(d_max<d[i]){d_max=d[i];r2=h[i];}
   }
   while(!q1.empty()){

     int i = q1.top().second;q1.pop();
     if(h[i]==0){continue;}
     res+=h[i]; h[i]=0;

     int next = (i+1==n)?0:i+1;

     while(h[next]!=0){

         h[next]=max(0ll,h[next]-d[i]);

         if(h[next]!=0){
             q1.push(mp(h[next],next));
             break;
         }
         i=next; next=(i+1==n)?0:i+1;
     }

   }
   cout<<min(res,r2)<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F;
int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}

int check(int i,vector<int>&h1,vector<int>&d1){
    vector<int>h=h1;
    vector<int>d=d1;
    int res=0; int sum = 0; int n=h.size();
    int next=(i+1==n)?0:i+1;

    while(!(h[i]==0&&h[next]==0)){
       // cout<<h[i]<<" "<<i<<endl;
         res+=h[i];h[i]=0;
         h[next]=max(0ll,h[next]-d[i]);
         i=next;  next=(i+1==n)?0:i+1;
    }

    return res;
}

void solve(){
  int n;cin>>n;

  vector<int>d(n);
  vector<int>h(n);
  ff(n){cin>>h[i]>>d[i];} int dif = LMAX; int index=-1;

  for(int i=0;i<n;i++){
     int h1 = h[(i+1)%n];
     int d1 = min(h1,d[i]);
     if(d1<dif){dif=d1;index=(i+1)%n;}
  }
  //cout<<index<<endl;
  cout<<check(index,h,d)<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}
int C(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    int ans = 1;
    int i;

    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }

    return ans;
}
//Round off to nth precision
 void print_precision(long double x,int y){
     x*= (pow(10,y));
     x=round(x);
     x/=pow(10,y);
     cout<<fixed<<setprecision(y)<<x<<endl;
 }

void solve(){
   int n;cin>>n;
   vector<int>h(n);
   vector<int>d(n);
   priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q1;
   ff(n){cin>>h[i]>>d[i];q1.push(mp(h[i],i));}
   int res=0;

   while(!q1.empty()){

       int index=q1.top().second;
       int val= q1.top().first;
      // cout<<val<<endl;
       q1.pop();  if(h[index]==0){continue;} //cehck for duplicates in pq

       res+=val; h[index]=0;
       int next=index+1;

      // cout<<next<<" "<<h[next]<<" "<<d[index]<<endl;
       next = (index+1==n)?0:index+1;
       while(h[next]!=0){
        //next = (index+1==n)?0:index+1;
        h[next]=max(0ll,h[next]-d[index]);
        if(h[next]!=0){q1.push(mp(h[next],next));}
        index=next; next = (index+1==n)?0:index+1;
       }

   }
   cout<<res<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F;
int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}

int check(int i,vector<int>&h1,vector<int>&d1){
    vector<int>h=h1;
    vector<int>d=d1;
    int res=0; int sum = 0; int n=h.size();
    int next=(i+1==n)?0:i+1;

    while(!(h[i]==0&&h[next]==0)){
       // cout<<h[i]<<" "<<i<<endl;
         res+=h[i];h[i]=0;
         h[next]=max(0ll,h[next]-d[i]);
         i=next;  next=(i+1==n)?0:i+1;
    }

    return res;
}

void solve(){
  int n;cin>>n;

  vector<int>d(n);
  vector<int>h(n);
  ff(n){cin>>h[i]>>d[i];} int dif = LMAX; int index=-1;

  for(int i=0;i<n;i++){
     int h1 = h[(i+1)%n];
     int d1 = min(h1,d[i]);
     if(d1<dif){dif=d1;index=(i+1)%n;}
  }
  cout<<index<<endl;
  cout<<check(index,h,d)<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}
int C(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    int ans = 1;
    int i;

    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }

    return ans;
}
//Round off to nth precision
 void print_precision(long double x,int y){
     x*= (pow(10,y));
     x=round(x);
     x/=pow(10,y);
     cout<<fixed<<setprecision(y)<<x<<endl;
 }
int check_for(int i,vector<int>&d,vector<int>&h){
    int next = (i+1==d.size())?0:i+1;
    int diff = h[i] - min(h[next],d[i]);
    return diff;
}
void solve(){
   int n;cin>>n;
   vector<int>h(n);
   vector<int>d(n);
   priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q1;
   ff(n){cin>>h[i]>>d[i];}
   ff(n){
       int diff =check_for(i,d,h);
       q1.push(mp(diff,i));
   }
   vector<bool>vis(n,false);
   int res=0; int d_max=-1;

   while(!q1.empty()){

     int i = q1.top().second;
     q1.pop();
     if(vis[i]){continue;}
     vis[i]=true;
     //if(h[i]==0){continue;}
     //cout<<i<<" "<<h[i]<<endl;

      res+=h[i];h[i]=0;
      int next = (i+1==n)?0:i+1;

      while(h[next]!=0){
      if(h[next]==0){continue;}
      h[next]=max(0ll,h[next]-d[i]);

      if(h[next]!=0){
          q1.push(mp(check_for(next,d,h),next));
          break;
      }
      vis[next]=true;
      i=next;next=(i+1==n)?0:i+1;
      }

   }
   cout<<(res)<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}

int check(int i,vector<int>&h,vector<int>&d){
    int res=0; int sum = 0; int n=h.size();
    int next=(i+1==n)?0:i+1;

    while(!(h[i]==0&&h[next]==0)){
         res+=h[i];h[i]=0;
         h[next]=max(0ll,h[next]-d[i]);
         i=next;  next=(i+1==n)?0:i+1;
    }

    return res;

}

void solve(){
  int n;cin>>n;

  vector<int>d(n);
  vector<int>h(n);
  int si=0; int min1=INT_MAX;
  ff(n){cin>>h[i]>>d[i]; min1=min(min1,h[i]);}

  vector<int>t;
  for(int i=0;i<n;i++){
      if(h[i]==min1){t.pb(i);}
  }
  int res=INT_MAX;
  for(auto i:t){
      res=min(res,check(i,h,d));
  }
  cout<<res<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}

int check(int i,vector<int>&h1,vector<int>&d1){
    vector<int>h=h1;
    vector<int>d=d1;
    int res=0; int sum = 0; int n=h.size();
    int next=(i+1==n)?0:i+1;

    while(!(h[i]==0&&h[next]==0)){
         res+=h[i];h[i]=0;
         h[next]=max(0ll,h[next]-d[i]);
         i=next;  next=(i+1==n)?0:i+1;
    }
    return res;
}

void solve(){
  int n;cin>>n;

  vector<int>d(n);
  vector<int>h(n);
  int si=0; int min1=INT_MAX;
  ff(n){cin>>h[i]>>d[i]; min1=min(min1,h[i]);}

  vector<int>t;
  for(int i=0;i<n;i++){
      if(h[i]==min1){t.pb(i);}
  }
  int res=INT_MAX;
  for(auto i:t){
      res=min(res,check(i,h,d));
  }
  cout<<res<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}

int check(int i,vector<int>&h1,vector<int>&d1){
    vector<int>h=h1;
    vector<int>d=d1;
    int res=0; int sum = 0; int n=h.size();
    int next=(i+1==n)?0:i+1;

    while(!(h[i]==0&&h[next]==0)){
       // cout<<h[i]<<" "<<i<<endl;
         res+=h[i];h[i]=0;
         h[next]=max(0ll,h[next]-d[i]);
         i=next;  next=(i+1==n)?0:i+1;
    }

    return res;
}

void solve(){
  int n;cin>>n;

  vector<int>d(n);
  vector<int>h(n);
  ff(n){cin>>h[i]>>d[i];} int dif = INT_MAX; int index=-1;

  for(int i=0;i<n;i++){
     int h1 = h[(i+1)%n];
     int d1 = min(h1,d[i]);
     if(d1<dif){dif=d1;index=(i+1)%n;}
  }
  cout<<check(index,h,d)<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define qq cout<<" here ";
#define int long long
#define ff(n) for(int i=0;i<n;i++)
#define For(i, a, b) for(ll i=(a);i<=(b);i++)
#define pb(i) push_back(i)
#define ld long double
#define mp(i,j) make_pair(i,j)
#define vi vector<ll>
ld pi=	3.14159265358979323846;
//int mod=1e9+7; const int N=1e6+5; int INF=0x3F3F3F3F; int LMAX= LLONG_MAX;
const int N = 1e5+5;
const int M = 1e9+7;

int absl(int x,int y){
    if(x>y){return x-y;}
    return y-x;
}
int powf(int a,int m){

    if(m<=0){return 1;}
    int t=powf(a,m/2); t=(t*t)%M;
    if(m&1){return (a*t)%M;}
    return t%M;
}
int C(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    int ans = 1;
    int i;

    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }

    return ans;
}
//Round off to nth precision
 void print_precision(long double x,int y){
     x*= (pow(10,y));
     x=round(x);
     x/=pow(10,y);
     cout<<fixed<<setprecision(y)<<x<<endl;
 }
int check_for(int i,vector<int>&d,vector<int>&h){
    int next = (i+1==d.size())?0:i+1;
    int diff = h[i] - min(h[next],d[i]);
    return diff;
}
void solve(){
   int n;cin>>n;
   vector<int>h(n);
   vector<int>d(n);
   priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q1;
   ff(n){cin>>h[i]>>d[i];}
   ff(n){
       int diff =check_for(i,d,h);
       q1.push(mp(diff,i));
   }
   vector<bool>vis(n,false);
   int res=0; int d_max=-1;

   while(!q1.empty()){

     int i = q1.top().second;
     q1.pop();
     if(vis[i]){continue;}
     vis[i]=true;
     //if(h[i]==0){continue;}
     //cout<<i<<" "<<h[i]<<endl;

     res+=h[i];h[i]=0;

      int next = (i+1==n)?0:i+1;
      if(h[next]==0){continue;}
      h[next]=max(0ll,h[next]-d[i]);
      int diff = check_for(next,d,h);
      q1.push(mp(diff,next));
   }
   cout<<(res)<<endl;

}
 main(){

    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

        int tc; tc=1;
        cin>>tc;
        while(tc--){
            solve();
        }
    }


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ll t;cin>>t;
	while(t--){
	    ll n;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        ll j=0;
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000,g1=0,sum=a[i].first;
        j=1;
	    while(i!=n){
	        ll g=max(g1,(a[j].first-a[j-1].second));
            sum+=g;
            j++;
            if(j-i==n+1){
                i++;
                j=i+1;
                h=min(h,sum);
                sum=a[i].first;
            }
	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;cin>>t;
	while(t--){

	    ll n,j=0;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000,g1=0,sum=a[i].first;
        j=0;
	    while(i!=n){
	        if(j-i!=n-1){
                j++;
                ll g=max(g1,(a[j].first-a[j-1].second));
                sum+=g;
            }
            else if(j-i==n-1){
                sum-=a[i].first;
                sum-=max(g1,(a[i+1].first-a[i].second));
                sum+=a[i+1].first;
                sum+=max(g1,(a[j+1].first-a[j].second));
                h=min(h,sum);
                i++;
                j++;
            }
	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ll t;cin>>t;
	while(t--){
	    ll n;cin>>n;
        pair<ll,ll>a[n];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
	    sort(a,a+n);
        ll sum=a[0].first;
	     for(ll i=1;i<n;i++){
	        if(a[i].first<=a[i-1].second) sum=sum;
            else sum+=(a[i].first-a[i-1].second);
	    }
        cout<<sum<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ll t;cin>>t;
	while(t--){
	    ll n;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        ll j=0;
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000,g1=0,sum=a[i].first;
        j=1;
	    while(i!=n){
	        ll g=max(g1,(a[j].first-a[j-1].second));

            sum+=g;
            j++;
            if(j-i==n){
                i++;
                j=i+1;
                h=min(h,sum);
                sum=a[i].first;
            }

	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;cin>>t;
	while(t--){

	    ll n,j=0;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=1000000000000000000,g1=0,sum=a[i].first;
        j=0;
	    while(i!=n){
	        if(j-i!=n-1){
                j++;
                ll g=max(g1,(a[j].first-a[j-1].second));
                sum+=g;
            }
            else if(j-i==n-1){
                sum-=a[i].first;
                sum-=max(g1,(a[i+1].first-a[i].second));
                sum+=a[i+1].first;
                sum+=max(g1,(a[j+1].first-a[j].second));
                h=min(h,sum);
                i++;
                j++;
            }
	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;cin>>t;
	while(t--){

	    ll n;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        ll j=0;
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000000,g1=0,sum=a[i].first;
        j=0;
	    while(i!=n){
	        if(j-i!=n-1){
                j++;
                ll g=max(g1,(a[j].first-a[j-1].second));
                sum+=g;
            }
            else if(j-i==n-1){
                sum-=a[i].first;
                sum-=max(g1,(a[i+1].first-a[i].second));
                sum+=a[i+1].first;
                sum+=max(g1,(a[j+1].first-a[j].second));
                h=min(h,sum);
                i++;
                j++;
            }
	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;cin>>t;
	while(t--){
	    ll n;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        ll j=0;
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000000,g1=0,sum=a[i].first;
        j=1;
	    while(i!=n){
	        ll g=max(g1,(a[j].first-a[j-1].second));

            sum+=g;
            j++;
            if(j-i==n){
                i++;
                j=i+1;
                h=min(h,sum);
                sum=a[i].first;
            }

	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ll t;cin>>t;
	while(t--){
	    ll n;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        ll j=0;
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000,g1=0,sum=a[i].first;
        j=1;
	    while(i!=n){
	        ll g=max(g1,(a[j].first-a[j-1].second));
            sum+=g;
            j++;
            if(j-i==n+1){
                i++;
                j=i;
                h=min(h,sum);
                sum=a[i].first;
            }

	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;cin>>t;
	while(t--){

	    ll n,j=0;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=100000000000000000,g1=0,sum=a[i].first;
        j=0;
	    while(i!=n){
	        if(j-i!=n-1){
                j++;
                ll g=max(g1,(a[j].first-a[j-1].second));
                sum+=g;
            }
            else if(j-i==n-1){
                sum-=a[i].first;
                sum-=max(g1,(a[i+1].first-a[i].second));
                sum+=a[i+1].first;
                sum+=max(g1,(a[j+1].first-a[j].second));
                h=min(h,sum);
                i++;
                j++;
            }
	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ll t;cin>>t;
	while(t--){
	    ll n;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        ll j=0;
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000,g1=0,sum=a[i].first;
        j=0;
	    while(i!=n){
	        if(j-i!=n-1){
                j++;
                ll g=max(g1,(a[j].first-a[j-1].second));
                sum+=g;
            }
            else if(j-i==n-1){
                sum-=a[i].first;
                sum-=max(g1,(a[i+1].first-a[i].second));
                sum+=a[i+1].first;
                sum+=max(g1,(a[j+1].first-a[j].second));
                h=min(h,sum);
                i++;
                j++;
            }
	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;cin>>t;
	while(t--){

	    ll n,j=0;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000000,g1=0,sum=a[i].first;
        j=0;
	    while(i!=n){
	        if(j-i!=n-1){
                j++;
                ll g=max(g1,(a[j].first-a[j-1].second));
                sum+=g;
            }
            else if(j-i==n-1){
                sum-=a[i].first;
                sum-=max(g1,(a[i+1].first-a[i].second));
                sum+=a[i+1].first;
                sum+=max(g1,(a[j+1].first-a[j].second));
                h=min(h,sum);
                i++;
                j++;
            }
	    }
        cout<<h<<"\n";
	}
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;cin>>t;
	while(t--){

	    ll n;cin>>n;
        ll n1=n*2;
        pair<ll,ll>a[n1];
	    for(ll i=0;i<n;i++)
	    {
	        ll x,y;
	        cin>>x>>y;
	        a[i].first=x;a[i].second=y;
	    }
        ll j=0;
        for(ll i=n;i<n1;i++){
            a[i].first=a[j].first;
            a[i].second=a[j].second;
            j++;
        }
        ll i=0,h=10000000000,g1=0,sum=a[i].first;
        j=0;
	    while(i!=n){
	        if(j-i!=n-1){
                j++;
                ll g=max(g1,(a[j].first-a[j-1].second));
                sum+=g;
            }
            else if(j-i==n-1){
                sum-=a[i].first;
                sum-=max(g1,(a[i+1].first-a[i].second));
                sum+=a[i+1].first;
                sum+=max(g1,(a[j+1].first-a[j].second));
                h=min(h,sum);
                i++;
                j++;
            }
	    }
        cout<<h<<"\n";
	}
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define endl '\n'
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 9;

int main(void)
{
    fastio
    test
    {
        int n;
        cin>>n;
        ll a[n+1],b[n+1],smalla = INT_MAX;
        for(int i=1; i<=n; i++)
        {
            cin >> a[i] >> b[i];
            smalla = min(smalla, a[i]);
        }

        ll d[n+1],x[n+1];
        ll cnt = 0;
        for(int i=1; i<=n; i++)
        {
            if(b[i] >= a[i%n + 1]);
            else
            {
                d[cnt] = a[i%n + 1] - b[i];
                x[cnt] = a[i%n + 1];
                cnt++;

            }
        }

        if(n == 2)
        {
            if(cnt == 0)
            {
                cout << min(a[1], a[2])<<endl;
            }
            else if(cnt == 1 && b[1] >= a[2])
            {
                cout << min(a[1],a[2]+d[0]) << endl;
            }
            else if(cnt == 1 && b[2] >= a[1])
            {
                cout << min(a[2],a[1]+d[0]) << endl;
            }
            else if(cnt == 2)
            {
                cout<< min(a[1] + d[0], a[2] + d[1]) << endl;
            }
            continue;
        }

        ll sumd = 0,f=1;
        for(int i=0; i<cnt; i++)
        {
            sumd += d[i];
            if(x[i] == smalla)
                f=0;
        }

        ll ans;
        if(f)
        ans = smalla + sumd;
        else
        ans = INT_MAX;

        for(int i=0; i<cnt; i++)
        {
            ans = min(ans,x[i] + sumd-d[i]);
        }

        cout << ans <<endl;

    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define endl '\n'
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 9;

int main(void)
{
    fastio
    test
    {
        int n;
        cin>>n;
        ll a[n+1],b[n+1],smalla = 1e18;
        for(int i=1; i<=n; i++)
        {
            cin >> a[i] >> b[i];
            smalla = min(smalla, a[i]);
        }

        ll d[n+1],x[n+1];
        ll cnt = 0;
        for(int i=1; i<=n; i++)
        {
            if(b[i] >= a[i%n + 1]);
            else
            {
                d[cnt] = a[i%n + 1] - b[i];
                x[cnt] = a[i%n + 1];
                cnt++;

            }
        }

        ll sumd = 0,f=1;
        for(int i=0; i<cnt; i++)
        {
            sumd += d[i];
            if(x[i] == smalla)
                f=0;
        }

        ll ans;
        if(f)
        ans = smalla + sumd;
        else
        ans = 1e18;

        for(int i=0; i<cnt; i++)
        {
            ans = min(ans,x[i] + sumd-d[i]);
        }

        cout << ans <<endl;

    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define endl '\n'
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 9;

int main(void)
{
    fastio
    test
    {
        int n;
        cin>>n;
        ll a[n+1],b[n+1],smalla = 1e18;
        for(int i=1; i<=n; i++)
        {
            cin >> a[i] >> b[i];
            smalla = min(smalla, a[i]);
        }

        ll d[n+1],x[n+1];
        ll cnt = 0;
        for(int i=1; i<=n; i++)
        {
            if(b[i] >= a[i%n + 1]);
            else
            {
                d[cnt] = a[i%n + 1] - b[i];
                x[cnt] = a[i%n + 1];
                cnt++;

            }
        }

        if(n == 2)
        {
            if(cnt == 0)
            {
                cout << min(a[1], a[2])<<endl;
            }
            else if(cnt == 1 && b[1] >= a[2])
            {
                cout << min(a[1],a[2]+d[0]) << endl;
            }
            else if(cnt == 1 && b[2] >= a[1])
            {
                cout << min(a[2],a[1]+d[0]) << endl;
            }
            else if(cnt == 2)
            {
                cout<< min(a[1] + d[0], a[2] + d[1]) << endl;
            }
            continue;
        }

        ll sumd = 0,f=1;
        for(int i=0; i<cnt; i++)
        {
            sumd += d[i];
            if(x[i] == smalla)
                f=0;
        }

        ll ans;
        if(f)
        ans = smalla + sumd;
        else
        ans = 1e18;

        for(int i=0; i<cnt; i++)
        {
            ans = min(ans,x[i] + sumd-d[i]);
        }

        cout << ans <<endl;

    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define mod 1000000007
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int main(void)
{
    fastio
    test
    {
        ll n,x,y,mn = INT_MAX;
        cin >> n;
        pair<ll, ll> p[300005];
        for(int i=0; i<n; i++)
        {
            cin >> x >> y;
            p[i] = {x,y};
            mn = min(mn, x);
        }

        int c = 0;
        ll cnt = 0;
        while(1)
        {
            if(c == n-1)
            {
                if(p[c].second >= p[0].first)
                {
                c++;
                }
                else
                {
                cnt += p[0].first - p[c].second;
                }
                break;
            }
            if(p[c].second >= p[c+1].first)
            {
                c++;
                continue;
            }
            else
            {
                cnt += p[c+1].first - p[c].second;
                c++;
            }
        }
        cout << mn + cnt << "\n";
    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define mod 1000000007
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int main(void)
{
    fastio
    test
    {
        ll n,x,y,mn = INT_MAX,minn = INT_MAX;
        cin >> n;
        pair<ll, ll> p[300005];
        for(int i=0; i<n; i++)
        {
            cin >> x >> y;
            p[i] = {x,y};
            minn = min(minn, x);

        }

        int c = 0;
        ll cnt = 0,t = 0;

        while(1)
        {
            if(c == n-1)
            {
                if(p[c].second >= p[0].first)
                {}
                else
                {
                t++;
                cnt += p[0].first - p[c].second;
                mn = min(mn, cnt);
                }
                break;
            }
            if(p[c].second >= p[c+1].first)
            {
                c++;
                continue;
            }
            else
            {
                t++;
                cnt += p[c+1].first - p[c].second;
                mn = min(mn, cnt);
                c++;
            }
        }

        if(t == 0) cout << minn << "\n";
        else cout << cnt + minn << "\n";
    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define mod 1000000007
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int main(void)
{
    fastio
    test
    {
        ll n,x,y,mn = INT_MAX,minn = INT_MAX,index,temp;
        cin >> n;
        pair<ll, ll> p[300005];
        for(int i=0; i<n; i++)
        {
            cin >> x >> y;
            p[i] = {x,y};
            minn = min(minn, x);
            index = i;

        }

        int c = 0;
        ll cnt = 0,t = 0;

        while(1)
        {
            if(c == index)
            {
                cnt -= temp;
            }
            if(c == n-1)
            {
                if(p[c].second >= p[0].first)
                {}
                else
                {
                t++;
                temp = p[0].first - p[c].second;
                cnt += temp;
                mn = min(mn, cnt);
                }
                break;
            }
            if(p[c].second >= p[c+1].first)
            {
                c++;
                continue;
            }
            else
            {
                t++;
                temp = p[c+1].first - p[c].second;
                cnt += temp;
                mn = min(mn, cnt);
                c++;
            }
        }

        if(t == 0) cout << minn << "\n";
        else cout << cnt + minn << "\n";
    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define endl '\n'
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 9;

int main(void)
{
    fastio
    test
    {
        int n;
        cin>>n;
        ll a[n+1],b[n+1],smalla = INT_MAX;
        for(int i=1; i<=n; i++)
        {
            cin >> a[i] >> b[i];
            smalla = min(smalla, a[i]);
        }

        ll d[n+1],x[n+1];
        ll cnt = 0;
        for(int i=1; i<=n; i++)
        {
            if(b[i] >= a[i%n + 1]);
            else
            {
                d[cnt] = a[i%n + 1] - b[i];
                x[cnt] = a[i%n + 1];
                cnt++;

            }
        }

        ll sumd = 0,f=1;
        for(int i=0; i<cnt; i++)
        {
            sumd += d[i];
            if(x[i] == smalla)
                f=0;
        }

        ll ans;
        if(f)
        ans = smalla + sumd;
        else
        ans = INT_MAX;

        for(int i=0; i<cnt; i++)
        {
            ans = min(ans,x[i] + sumd-d[i]);
        }

        cout << ans <<endl;

    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define mod 1000000007
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int main(void)
{
    fastio
    test
    {
        ll n,x,y,minn = INT_MAX,index,temp;
        cin >> n;
        pair<ll, ll> p[300005];
        for(int i=0; i<n; i++)
        {
            cin >> x >> y;
            p[i] = {x,y};
            minn = min(minn, x);
            index = i;

        }

        int c = 0;
        ll cnt = 0,t = 0;

        while(1)
        {
            if(c == index && index != 0)
            {
                cnt -= temp;
            }
            if(c == n-1)
            {
                if(p[c].second >= p[0].first)
                {}
                else
                {
                temp = p[0].first - p[c].second;
                cnt += temp;
                }
                break;
            }
            if(p[c].second >= p[c+1].first)
            {
                c++;
                continue;
            }
            else
            {
                temp = p[c+1].first - p[c].second;
                cnt += temp;
                c++;
            }
        }
        if(index == 0)
        {
            cnt -= temp;
        }

         cout << cnt + minn << "\n";
    }
    return 0;
}


//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define digits(x) floor(log10(n) + 1)
#define mod 1000000007
#define pn(x) cout << x <<"\n";
#define ps(x) cout << x <<" ";
#define deb(x) cout << #x << "=" << x << endl;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int main(void)
{
    fastio
    test
    {
        ll n,x,y,minn = INT_MAX,index,temp = INT_MAX,fg = 0;
        cin >> n;
        pair<ll, ll> p[300005];
        for(int i=0; i<n; i++)
        {
            cin >> x >> y;
            p[i] = {x,y};
            minn = min(minn, x);
            index = i;

        }

        int c = 0;
        ll cnt = 0,t = 0;

        while(1)
        {
            if(c == index && temp != INT_MAX)
            {
                cnt -= temp;
                fg = 1;
            }
            if(c == n-1)
            {
                if(p[c].second >= p[0].first)
                {}
                else
                {
                temp = p[0].first - p[c].second;
                cnt += temp;
                }
                break;
            }
            if(p[c].second >= p[c+1].first)
            {
                c++;
                continue;
            }
            else
            {
                temp = p[c+1].first - p[c].second;
                cnt += temp;
                c++;
            }
        }
        if(fg == 0)
        {
            cnt -= temp;
        }

         cout << cnt + minn << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n;
    cin>>n;
    ll a[n],b[n];
    ll total=0,z=0;
    for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
            if(i>=1)
               {
                   z=a[(i%n)]-b[(i-1)%n] ;
                   if(z<0)
                       z=0;

                   total+=z;
               }

        }
    if(a[0]-b[n-1] >0)
       total+= a[0]-b[n-1] ;

    ll min=0;
    ll c=0;
    ll l=n ;
    for(int i=0;i<n;i++)
       {   z=b[i%l] -a[((i+1)%l)] ;
           if(z>0)
              z=0;
            else z= -z;
           //cout<<z<<endl;
           if(i==0)
             {
                 min=a[1%l] + total - z;
                 //cout<<min<<endl;
             }
            else
            {
                 c= a[(i+1)%l] + total-z;
                 if(min>c)
                   min=c;
            }
       }

    cout<<min<<endl;
}

int main()
{
  int t;
  cin>>t;
  while(t--)
      solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n;
    cin>>n;
    ll a[n],b[n];
    ll total=0,z=0;
    for(ll i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
            if(i>=1)
               {
                   z=a[(i%n)]-b[(i-1)%n] ;
                   if(z<0)
                       z=0;

                   total+=z;
               }

        }
    if(a[0]-b[n-1] >0)
       total+= a[0]-b[n-1] ;

    ll min=0;
    ll c=0;
    ll l=n ;
    for(ll i=0;i<n;i++)
       {   z=b[i%l] -a[((i+1)%l)] ;
           if(z>0)
              z=0;
            else z= -z;
           //cout<<z<<endl;
           if(i==0)
             {
                 min=a[1%l] + total - z;
                 //cout<<min<<endl;
             }
            else
            {
                 c= a[(i+1)%l] + total-z;
                 if(min>c)
                   min=c;
            }
       }

    cout<<min<<endl;
}

int main()
{
  int t;
  cin>>t;
  while(t--)
      solve();
}


#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin>>n;
    vector<pair<int ,int>>vt;
    for(int i=0;i<n;i++)
        {   int x,y ;
            cin>>x>>y;
            vt.push_back(make_pair(x,y)) ;
        }

    int l=vt.size();
    int total=0,min=0;
    int z=0;
    for(int i=0;i<l;i++)
       {
            z=vt[i%l].second -vt[(i+1)%l].first ;
           if(z>0)
              {
                  z=0;
              }
           else
              z= -z ;

           total+=z ;
       }

    for(int i=0;i<l;i++)
       {   z=vt[i%l].second -vt[((i+1)%l)].first ;
           if(z>0)
              z=0;
            else z= -z;
           if(i==0)
             {
                 min=vt[1%l].first + total - z;
             }
            else
            {
                int c= vt[(i+1)%l].first + total-z;
                if(min>c)
                   min=c;
            }
       }

    cout<<min<<endl;
}

int main()
{
  int t;
  cin>>t;
  while(t--)
      solve();
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); ++i)

typedef long long li;

const int N = 300 * 1000 + 13;

int n;
li a[N], b[N];

void solve() {
	scanf("%d", &n);
	forn(i, n) scanf("%lld%lld", &a[i], &b[i]);

	li ans = 0, mn = 1e18;
	forn(i, n) {
		int ni = (i + 1) % n;
		li val = min(a[ni], b[i]);
		ans += a[ni] - val;
		mn = min(mn, val);
	}
	ans += mn;
	printf("%lld\n", ans);
}

int main() {
	int T;
	scanf("%d", &T);
	while(t--)
		solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n;
    cin>>n;
    vector<pair<ll ,ll>>vt;
    for(int i=0;i<n;i++)
        {   ll x,y ;
            cin>>x>>y;
            vt.push_back(make_pair(x,y)) ;
        }

    ll l=vt.size();
    ll total=0,min=0;
    ll z=0,c=0;

    for(int i=0;i<l;i++)
       {
            z=vt[i%l].second -vt[(i+1)%l].first ;
           if(z>0)
              {
                  z=0;
              }
           else
              z= -z ;

           total+=z ;
       //   cout<<total<<endl;
       }

    for(int i=0;i<l;i++)
       {   z=vt[i%l].second -vt[((i+1)%l)].first ;
           if(z>0)
              z=0;
            else z= -z;
           //cout<<z<<endl;
           if(i==0)
             {
                 min=vt[1%l].first + total - z;
                 //cout<<min<<endl;
             }
            else
            {
                 c= vt[(i+1)%l].first + total-z;
                 if(min>c)
                   min=c;
                cout<<min<<endl;
            }
       }

    //cout<<min<<endl;
}

int main()
{
  int t;
  cin>>t;
  while(t--)
      solve();
}


#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin>>n;
    vector<pair<int ,int>>vt;
    for(int i=0;i<n;i++)
        {   int x,y ;
            cin>>x>>y;
            vt.push_back(make_pair(x,y)) ;
        }

    int l=vt.size();
    int total=0,min=0;
    for(int i=0;i<l;i++)
       {
           int z=vt[i%l].second -vt[(i+1)%l].first ;
           if(z>0)
              total+=0;
           else
              total+= abs(z) ;
       }

    for(int i=0;i<l;i++)
       {   int z=vt[i%l].second -vt[((i+1)%l)].first ;
           if(z>0)
              z=0;
            else z= -z;
           if(i==0)
             {
                 min=vt[1%l].first + total - z;
             }
            else
            {
                int c= vt[(i+1)%l].first + total-z;
                if(min>c)
                   min=c;
            }
       }

    cout<<min<<endl;
}

int main()
{
  int t;
  cin>>t;
  while(t--)
      solve();
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); ++i)

typedef long long li;

const int N = 300 * 1000 + 13;

int n;
li a[N], b[N];

void solve() {
	scanf("%d", &n);
	forn(i, n) scanf("%lld%lld", &a[i], &b[i]);

	li ans = 0, mn = 1e18;
	forn(i, n) {
		int ni = (i + 1) % n;
		li val = min(a[ni], b[i]);
		ans += a[ni] - val;
		mn = min(mn, val);
	}
	ans += mn;
	printf("%lld\n", ans);
}

int main() {
	int T;
	scanf("%d", &T);
     while(T--)
		solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n;
    cin>>n;
    ll a[n],b[n];
    ll total=0,z=0,p=0,m=0;
    for(ll i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
            if(i==0)
                m=a[0];
            if(i>=1)
               {
                   z=a[(i%n)]-b[(i-1)%n] ;
                   if(z<0)
                       z=0;

                   total+=z;

                    if(m>a[i])
                       {
                          m=a[i];
                          p=i;
                       }
               }

        }
    if(a[0]-b[n-1] >0)
       total+= a[0]-b[n-1] ;
      ll ans=0;
   if(p==0)
      ans= m + total- (  a[0]-min(b[n-1],a[0]) ) ;
   else
      ans= m+ total -(a[p%n]-min(b[(p-1)%n],a[p%n]) );
  cout<<ans<<endl;
}

int main()
{
  int t;
  cin>>t;
  while(t--)
      solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n;
    cin>>n;
    vector<pair<ll ,ll>>vt;
    for(int i=0;i<n;i++)
        {   ll x,y ;
            cin>>x>>y;
            vt.push_back(make_pair(x,y)) ;
        }

    ll l=vt.size();
    ll total=0,min=0;
    ll z=0,c=0;

    for(int i=0;i<l;i++)
       {
            z=vt[i%l].second -vt[(i+1)%l].first ;
           if(z>0)
              {
                  z=0;
              }
           else
              z= -z ;

           total+=z ;
       //   cout<<total<<endl;
       }

    for(int i=0;i<l;i++)
       {   z=vt[i%l].second -vt[((i+1)%l)].first ;
           if(z>0)
              z=0;
            else z= -z;
           //cout<<z<<endl;
           if(i==0)
             {
                 min=vt[1%l].first + total - z;
                 //cout<<min<<endl;
             }
            else
            {
                 c= vt[(i+1)%l].first + total-z;
                 if(min>c)
                   min=c;
            }
       }

    cout<<min<<endl;
}

int main()
{
  int t;
  cin>>t;
  while(t--)
      solve();
}