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

void solve()
{
    int 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;
    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>
#define int long long
#define FASTIO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int32_t main()
{
    FASTIO;
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<pair<int,int> >v;
        int x,y;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
        int diff,ind,total=0;
        int mn=1e18;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                diff=v[i].first-max(0LL,v[i].first-v[n-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
            else
            {
                diff=v[i].first-max(0LL,v[i].first-v[i-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
        }
        total+=v[ind].first;
        for(int i=ind+1;i<n;i++)
        {
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        for(int i=0;i<ind;i++)
        {
            if(i==0)
            {
                total+=max(0LL,v[i].first-v[n-1].second);
            }
            else
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        cout<<total<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<pair<int,int> >v;
        int x,y;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
        int diff,ind,total=0;
        int mn=INT_MAX;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                diff=v[i].first-max(0LL,v[i].first-v[n-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
            else
            {
                diff=v[i].first-max(0LL,v[i].first-v[i-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
        }
        total+=v[ind].first;
        for(int i=ind+1;i<n;i++)
        {
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        for(int i=0;i<ind;i++)
        {
            if(i==0)
            {
                total+=max(0LL,v[i].first-v[v.size()-1].second);
            }
            else
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        cout<<total<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<pair<int,int> >v;
        int x,y;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
        vector<pair<pair<int,int>,int> >vi;
        for(int i=0;i<v.size();i++)
        {
            vi.push_back({{v[i].first,v[i].second},i});
        }
        int diff;
        int mn=INT_MAX;
        int ind;
        int total=0;
        vector<pair<int,int> >store;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                diff=max(0LL,v[i].first-v[n-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    //ind=n-1;
                    store.clear();
                    store.push_back({v[n-1].first,v[n-1].second});
                }
            }
            else{
            diff=max(0LL,v[i].first-v[i-1].second);
            if(diff<mn)
            {
                mn=diff;
                //ind=i-1;
                store.clear();
                store.push_back({v[i-1].first,v[i-1].second});
            }
            else if(diff==mn)
            {
                store.push_back({v[i-1].first,v[i-1].second});
            }
            }
        }
        int mini=INT_MAX;
        int sp;
        for(int i=0;i<store.size();i++)
        {
            if(store[i].first<mini)
            {
                mini=store[i].first;
                sp=store[i].second;
            }
        }
        for(int i=0;i<vi.size();i++)
        {
            if(vi[i].first.first==mini && vi[i].first.second==sp)
            {
                ind=vi[i].second;
            }
        }
        total=mini;
        vector<pair<int,int> >res;
        for(int i=ind;i<n;i++)
        {
            res.push_back({v[i].first,v[i].second});
        }
        for(int i=0;i<ind;i++)
        {
            res.push_back({v[i].first,v[i].second});
        }
        for(int i=1;i<res.size();i++)
        {
            total+=(max(0LL,res[i].first-res[i-1].second));
        }
        cout<<total<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<pair<int,int> >v;
        int x,y;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
        int diff,ind,total=0;
        int mn=1e18;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                diff=v[i].first-max(0LL,v[i].first-v[n-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
            else
            {
                diff=v[i].first-max(0LL,v[i].first-v[i-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
        }
        total+=v[ind].first;
        for(int i=ind+1;i<n;i++)
        {
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        for(int i=0;i<ind;i++)
        {
            if(i==0)
            {
                total+=max(0LL,v[i].first-v[n-1].second);
            }
            else
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        cout<<total<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<pair<int,int> >v;
        int x,y;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
        int diff,ind,total=0;
        int mn=INT_MAX;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                diff=v[i].first-max(0LL,v[i].first-v[n-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
            else
            {
                diff=v[i].first-max(0LL,v[i].first-v[i-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
        }
        vector<pair<int,int> >res;
        for(int i=ind;i<n;i++)
        {
            res.push_back({v[i].first,v[i].second});
        }
        for(int i=0;i<ind;i++)
        {
            res.push_back({v[i].first,v[i].second});
        }
        total+=v[ind].first;
        for(int i=1;i<res.size();i++)
        {
            total+=max(0LL,res[i].first-res[i-1].second);
        }
        cout<<total<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n],b[n];
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
        }
        int mn=1e18;
        int total=0;
        for(int i=0;i<n;i++)
        {
            int ind=(i+1)%n;
            int val=min(a[ind],b[i]);
            total+=a[i]-val;
            mn=min(mn,val);
        }
        cout<<total+mn<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<pair<int,int> >v;
        int x,y;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
        int diff;
        int mn=INT_MAX;
        int ind;
        int total=0;
        vector<int>store;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                diff=max(0LL,v[i].first-v[n-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=n-1;
                    store.clear();
                    store.push_back(v[n-1].first);
                }
            }
            else{
            diff=max(0LL,v[i].first-v[i-1].second);
            if(diff<mn)
            {
                mn=diff;
                ind=i-1;
                store.clear();
                store.push_back(v[i-1].first);
            }
            else if(diff==mn)
            {
                store.push_back(v[i-1].first);
            }
            }
        }
        int mini=INT_MAX;
        for(int i=0;i<store.size();i++)
        {
            if(store[i]<mini)
            {
                mini=store[i];
                ind=i;

            }
        }
        total=mini;
        vector<pair<int,int> >res;
        for(int i=ind;i<n;i++)
        {
            res.push_back({v[i].first,v[i].second});
        }
        for(int i=0;i<ind;i++)
        {
            res.push_back({v[i].first,v[i].second});
        }
        for(int i=1;i<res.size();i++)
        {
            total+=(max(0LL,res[i].first-res[i-1].second));
        }
        cout<<total<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
#define FASTIO ios::sync_with_stdio(false);cin.tie(NULL),cout.tie(NULL);
using namespace std;
int32_t main()
{
    FASTIO;
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n],b[n];
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
        }
        int mn=1e18;
        int total=0;
        for(int i=0;i<n;i++)
        {
            int ind=(i+1)%n;
            int val=min(a[ind],b[i]);
            total+=a[i]-val;
            mn=min(mn,val);
        }
        cout<<total+mn<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<pair<int,int> >v;
        int x,y;
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
        int diff,ind,total=0;
        int mn=INT_MAX;
        for(int i=0;i<n;i++)
        {
            if(i==0)
            {
                diff=v[i].first-max(0LL,v[i].first-v[n-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
            else
            {
                diff=v[i].first-max(0LL,v[i].first-v[i-1].second);
                if(diff<mn)
                {
                    mn=diff;
                    ind=i;
                }
            }
        }
        total+=v[ind].first;
        for(int i=ind+1;i<n;i++)
        {
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        for(int i=0;i<ind;i++)
        {
            if(i==0)
            {
                total+=max(0LL,v[i].first-v[n-1].second);
            }
            else
            total+=max(0LL,v[i].first-v[i-1].second);
        }
        cout<<total<<endl;
    }
    return 0;
}


             /**`-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'**\
                    \      :          \          |:  :|          /         :       /
                     \     ::    .     `-_______/ :::: \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<climits>
using namespace std;
#define mx 3000007
#define LL long long
#define mod 1000000007
#define infinity 10000000000000007
LL arr[mx],brr[mx],chck_arr[mx],chck_brr[mx];

int main()
{
ios::sync_with_stdio(false); cin.tie(0);

LL test,n;

cin>>test;
while(test--)
{
    cin>>n;
    LL mini=infinity,indx;
    for(LL i=1;i<=n;++i)
    {
      cin>>arr[i]>>brr[i];chck_arr[i]=arr[i];chck_brr[i]=brr[i];
      if(chck_arr[i]<mini)
      {
      mini=chck_arr[i];indx=i;
      }
    }

    LL cnt=0,i=indx,ans=0,next_;

    while(true)
    {
    ans+=chck_arr[i];chck_arr[i]=0;next_=i+1;if(next_>n)next_-=n;
    mini=min(chck_arr[next_],chck_brr[i]);chck_arr[next_]-=mini;i=next_;if(i==indx)break;
    }

    mini=infinity;

    for(LL i=1;i<=n;++i)
    {
    chck_arr[i]=arr[i];chck_brr[i]=brr[i];
    next_=i+1;if(next_>n)next_-=n;
    if(chck_arr[next_]>chck_brr[i])
    {
    if(chck_arr[next_]<mini){mini=chck_arr[next_];indx=next_;}
    }
    }
    LL ans1=0;
    cnt=0;i=indx;
    while(true)
    {
    ans1+=(chck_arr[i]);next_=i+1;if(next_>n)next_-=n;
    mini=min(chck_arr[next_],chck_brr[i]);chck_arr[next_]-=mini;
    i=next_;if(i==indx)break;
    }

    cout<<min(ans1,ans)<<endl;
}

	return 0;
}


             /**`-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'**\
                    \      :          \          |:  :|          /         :       /
                     \     ::    .     `-_______/ :::: \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<climits>
using namespace std;
#define mx 300007
#define LL long long
#define mod 1000000007
#define infinity 10000000000000007
LL arr[mx],brr[mx],chck_arr[mx],chck_brr[mx];

int main()
{
ios::sync_with_stdio(false); cin.tie(0);

LL test,n;

cin>>test;
while(test--)
{
    cin>>n;
    LL mini=infinity,indx;
    for(LL i=1;i<=n;++i)
    {
      cin>>arr[i]>>brr[i];chck_arr[i]=arr[i];chck_brr[i]=brr[i];
      if(arr[i]<mini)
      {
          mini=arr[i];indx=i;
      }
    }

    LL cnt=0,i=indx,ans=0,next_;

    while(true)
    {
    if(i==indx)++cnt;if(cnt==2)break;
    ans+=chck_arr[i];chck_arr[i]=0;next_=i+1;if(next_>n)next_-=n;
    mini=min(chck_arr[next_],chck_brr[i]);chck_arr[next_]-=mini;i=next_;
    }

    mini=infinity;

    for(LL i=1;i<=n;++i)
    {
    chck_arr[i]=arr[i];chck_brr[i]=brr[i];
    next_=i+1;if(next_>n)next_-=n;
    if(chck_arr[next_]>chck_brr[i])
    {
    if(chck_arr[next_]<mini){mini=chck_arr[next_];indx=next_;}
    }
    }
    LL ans1=0;
    cnt=0;i=indx;
    while(true)
    {
    if(i==indx)++cnt;if(cnt==2)break;
    ans1+=(chck_arr[i]);next_=i+1;if(next_>n)next_-=n;
    chck_arr[next_]-=(min(chck_arr[next_],chck_brr[i]));
    i=next_;
    }
    cout<<min(ans,ans1)<<endl;

}

	return 0;
}


             /**`-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'**\
                    \      :          \          |:  :|          /         :       /
                     \     ::    .     `-_______/ :::: \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<climits>
using namespace std;
#define mx 300007
#define LL long long
#define mod 1000000007
#define infinity 10000000000000007
LL arr[mx],brr[mx],chck_arr[mx],chck_brr[mx];

int main()
{
ios::sync_with_stdio(false); cin.tie(0);

LL test,n;

cin>>test;
while(test--)
{
    cin>>n;
    LL mini=infinity,indx;
    for(LL i=1;i<=n;++i)
    {
      cin>>arr[i]>>brr[i];chck_arr[i]=arr[i];chck_brr[i]=brr[i];
      if(arr[i]<mini)
      {
          mini=arr[i];indx=i;
      }
    }

    LL cnt=0,i=indx,ans=0,next_;

    while(true)
    {
    if(i==indx)++cnt;if(cnt==2)break;
    ans+=chck_arr[i];chck_arr[i]=0;next_=i+1;if(next_>n)next_-=n;
    mini=min(chck_arr[next_],chck_brr[i]);chck_arr[next_]-=mini;i=next_;
    }

    mini=infinity;

    for(LL i=1;i<=n;++i)
    {
    chck_arr[i]=arr[i];chck_brr[i]=brr[i];
    next_=i+1;if(next_>n)next_-=n;
    if(chck_arr[next_]>chck_brr[i])
    {
    if(chck_arr[next_]<mini){mini=chck_arr[next_];indx=next_;}
    }
    }
    LL ans1=0;
    cnt=0;i=indx;
    while(true)
    {
    if(i==indx)++cnt;if(cnt==2)break;
    ans1+=(chck_arr[i]);next_=i+1;if(next_>n)next_-=n;
    chck_arr[next_]-=(min(chck_arr[next_],chck_brr[i]));
    i=next_;
    }
    if(ans1 && ans)
    ans=min(ans,ans1);
    else if(ans1)ans=ans1;
    else if(ans)ans=ans;
    cout<<ans<<endl;

}

	return 0;
}


             /**`-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'**\
                    \      :          \          |:  :|          /         :       /
                     \     ::    .     `-_______/ :::: \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<climits>
using namespace std;
#define mx 300007
#define LL long long
#define mod 1000000007
#define infinity 10000000000000007
LL arr[mx],brr[mx],chck_arr[mx],chck_brr[mx];

int main()
{
ios::sync_with_stdio(false); cin.tie(0);

LL test,n;

cin>>test;
while(test--)
{
    cin>>n;
    LL mini=infinity,indx;
    for(LL i=1;i<=n;++i)
    {
      cin>>arr[i]>>brr[i];chck_arr[i]=arr[i];chck_brr[i]=brr[i];
      if(arr[i]<mini)
      {
          mini=arr[i];indx=i;
      }
    }

    LL cnt=0,i=indx,ans=0,next_;

    while(true)
    {
    if(i==indx)++cnt;if(cnt==2)break;
    ans+=chck_arr[i];next_=i+1;if(next_>n)next_-=n;
    chck_arr[next_]-=(min(chck_arr[next_],chck_brr[i]));
    i=next_;
    }
    mini=infinity;

    for(LL i=1;i<=n;++i)
    {
    chck_arr[i]=arr[i];chck_brr[i]=brr[i];
    next_=i+1;if(next_>n)next_-=n;
    if(chck_arr[next_]>chck_brr[i])
    {
     if(chck_arr[next_]<mini){mini=chck_arr[next_];indx=i;}
    }
    }
    LL ans1=0;
    cnt=0;i=indx;
    while(true)
    {
    if(i==indx)++cnt;if(cnt==2)break;
    ans1+=(chck_arr[i]);next_=i+1;if(next_>n)next_-=n;
    chck_arr[next_]-=(min(chck_arr[next_],chck_brr[i]));
    i=next_;
    }
    cout<<min(ans,ans1)<<endl;

}

	return 0;
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
		for (int i = 0; i < n-2; ++i)
		{
			dp[i+1]=max(0LL,a[i+1]-b[i]);
		}
        ll sum=0;
		dp[0]=max(0LL,a[0]-b[n-1]);
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}
		ll ans=10000000000000;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
		for (int i = 0; i < n-1; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
		}
        if(a[0]-b[n-1]>0)
			dp[0]=a[0]-b[n-1];
        ll sum=0;
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}

		ll ans=10000000000000;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
		for (int i = 0; i < n-2; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
		}
        if(a[0]-b[n-1]>0)
			dp[0]=a[0]-b[n-1];
        ll sum=0;
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}

		ll ans=LLONG_MAX;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
        ll sum=0;
		for (int i = 0; i < n-1; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
            sum+=dp[i+1];
		}
        if(a[0]-b[n-1]>0)
        {
			dp[0]=a[0]-b[n-1];
            sum+=dp[0];
        }
		ll ans=LLONG_MAX;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			if(mid<ans)
                ans=mid;
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
		for (int i = 0; i < n-2; ++i)
		{
			dp[i+1]=max(0LL,a[i+1]-b[i]);
		}
		dp[0]=max(0LL,a[0]-b[n-1]);
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}
		ll ans=1000000000000;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		ll mn=INT_MAX;
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
			mn=min(mn,a[i]);
		}
		ll dp[n+1]={0};
		for (int i = 0; i < n-2; ++i)
		{
			dp[i+1]=max(0LL,a[i+1]-b[i]);
		}
		dp[0]=max(0LL,a[0]-b[n-1]);
        ll sum=0;
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}
		ll ans=INT_MAX;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
		for (int i = 0; i < n-2; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
		}
        if(a[0]-b[n-1]>0)
			dp[0]=a[0]-b[n-1];
        ll sum=0;
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}

		ll ans=10000000000000;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
		for (int i = 0; i < n-1; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
		}
        if(a[0]-b[n-1]>0)
        {
			dp[0]=a[0]-b[n-1];
        }
        ll sum=0;
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}

		ll ans=LLONG_MAX;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		ll mn=INT_MAX;
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
			mn=min(mn,a[i]);
		}
		//a[0]=a[n],b[0]=b[n];
		ll dp[n]={0};
		for (int i = 0; i < n-2; ++i)
		{
            //cout<<a[i+1]<<"  "<<b[i]<<endl;
			dp[i]=max(0LL,a[i+1]-b[i]);
		}
		dp[n-1]=max(0LL,a[0]-b[n-1]);
		ll sum=0;
       /* for (int i = 0; i < n; ++i)
		{
			cout<<dp[i]<<endl;
		}*/
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
				sum+=dp[i];
		}
		cout<<sum+mn<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
        ll sum=0;
		for (int i = 0; i < n-1; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
            sum+=dp[i+1];
		}
        if(a[0]-b[n-1]>0)
        {
			dp[0]=a[0]-b[n-1];
            sum+=dp[0];
        }

		ll ans=LLONG_MAX;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);

#define mod 1000000007
int main()
{
    fast;
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
        ll sum=0;
		for (int i = 0; i < n-1; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
            sum+=dp[i+1];
		}
        if(a[0]-b[n-1]>0)
        {
			dp[0]=a[0]-b[n-1];
            sum+=dp[0];
        }
		ll ans=LLONG_MAX;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			if(mid<ans)
                ans=mid;
		}
		cout<<ans<<endl;
	}
}


/*
CODED BY
**************
DARSHAN JAIN
darshanjain31
**************
*/
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define pb push_back
#define mod 1000000007
int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll a[n]={0},b[n]={0};
		for (int i = 0; i < n; ++i)
		{
			cin>>a[i]>>b[i];
		}
		ll dp[n]={0};
		for (int i = 0; i < n-1; ++i)
		{
            if(a[i+1]-b[i]>0)
			dp[i+1]=a[i+1]-b[i];
		}
        if(a[0]-b[n-1]>0)
			dp[0]=a[0]-b[n-1];
        ll sum=0;
		for (int i = 0; i < n; ++i)
		{
			if(dp[i]!=0)
            {
				sum+=dp[i];
            }
		}

		ll ans=10000000000000000;
		for (int i = 0; i < n; ++i)
		{
			ll mid=a[i]+sum-dp[i];
          //  cout<<mid<<endl;
			ans=min(ans,mid);
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=300010;
const int Max=1e18;
int health[N],xplode[N],tests,n;
signed main()
{
	cin>>tests;
	while(tests--)
	{
		int imin=Max,output=0;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>health[i]>>xplode[i];
		}
		for(int i=0;i<n;i++)
		{
			output+=max(0LL,health[i]-xplode[(i+n-1)%n]);
			imin=min(imin,health[i]-max(0LL,health[i]-xplode[(i+n-1)%n]));
		}
		cout<<output+imin<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
	int tests;
	cin>>tests;
	while(tests--)
	{
		int n,imin=1e18,output=0;
		cin>>n;
		int health[n],xplode[n];
		for(int i=0;i<n;i++)
		{
			cin>>health[i]>>xplode[i];
		}
		for(int i=0;i<n;i++)
		{
			output+=max(0,health[i]-xplode[(i+n-1)%n]);
			imin=min(imin,health[i]-max(0,health[i]-xplode[(i+n-1)%n]);
		}
	}
	cout<<output+imin<<endl;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=300001;
int health[N],xplode[N];
signed main()
{
	int tests;
	cin>>tests;
	while(tests--)
	{
		int n,imin=1e18,output=0;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>health[i]>>xplode[i];
		}
		for(int i=0;i<n;i++)
		{
			output+=max(0LL,health[i]-xplode[(i+n-1)%n]);
			imin=min(imin,health[i]-max(0LL,health[i]-xplode[(i+n-1)%n]));
		}
		cout<<output+imin<<endl;
	}
}


#include "iostream"
#include <string>
using namespace std;

int main() {
	string s;
	cin >> s;
	string sc = s;
	int rows = (s.length() + 19) / 20;
	int col = 0;
	if (s.length() % rows == 0) col = s.length() / rows;
	else col = (s.length() + rows - 1) / rows;

	cout << rows << " " << col << endl;
	int perlu = rows - s.length() % rows;
	if (perlu == rows) perlu == 0;
	int curind = 0;

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < col; j ++) {
			if ((j == col - 1) && (i<perlu)&& s.length()%rows>0)	cout << "*";
			cout << s[curind++];
		}

		cout << endl;
	}

	return 0;
}


#include "iostream"
#include <string>
using namespace std;

int main() {
	string s;
	cin >> s;
	string sc = s;
	int rows = (s.length() + 19) / 20;
	int col = s.length()/rows + (s.length()%rows>0);

	cout << rows << " " << col << endl;
	int perlu = rows - s.length() % rows;
	if (perlu == rows) perlu == 0;
	int curind = 0;

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < col; j ++) {
			if ((j == col - 1) && (i<perlu))	cout << "*";
			cout << s[curind++];
		}

		cout << endl;
	}

	return 0;
}


#include "iostream"
using namespace std;

int main()
{
	string s;
	cin>>s;
	int sz=s.size();
	//cout<<sz<<"\n";
	int r=(sz+19)/20;
	int c=sz/r + (sz%r>0);
	cout<<((sz+19)/20)<<" "<<c<<"\n";
	int perlu=r - sz%r;
	if(perlu==r)	perlu=0;
	int curind=0,temp,temp2;
	for(temp=0;temp<r;temp++)
	{
		for(temp2=0;temp2<c;temp2++)
		{
			if((temp2==c-1)&&(temp<perlu))	cout<<"*";
			else	cout<<s[curind++];
		}
		cout<<"\n";
	}
}


#include "iostream"
using namespace std;

int main()
{
	string s;
	cin>>s;
	int sz=s.size();
	//cout<<sz<<"\n";
	int rows=(sz+19)/20;
	int col=sz/rows + (sz%rows>0);
	cout<<((sz+19)/20)<<" "<<col<<"\n";
	int perlu=rows - sz%rows;
	if(perlu==rows)	perlu=0;
	int curind=0,i,j;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<col;j++)
		{
			if((j==col-1)&&(i<perlu))	cout<<"*";
			else	cout<<s[curind++];
		}
		cout<<endl;
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <assert.h>
using namespace std;

const int N = 110;
char s[110];
bool dp[6][25][110];
pair<int, int> pre[6][25][110];

void output(int r, int n, int m, int c) {
	if (!r) return;
	output(r - 1, pre[r][n][m].first, pre[r][n][m].second, c);
	for (int i = 1; i <= n; i++)
		printf("*");
	for (int j = pre[r][n][m].second + 1; j <=m; j++)
		printf("%c", s[j]);
	printf("\n");
}

int main()
{
	int n;
	scanf("%s", s + 1);
	n = strlen(s + 1);
	if (n <= 20) {
		printf("1 %d\n", n);
		cout << s + 1 << endl;
		return 0;
	}

    for (int r = 2; r <= 5; r++) {
		for (int c = 1; c <= 20; c++) {
			if (r * c < n) continue;
            memset(dp, false, sizeof(dp));
			for (int i = 0; i <= c; i++)
				dp[1][i][c - i] = true;

			for (int i = 1; i < r; i++)
			    for (int j = 0; j <= c; j++)
			        for (int k = 0; k <= n; k++) {
				        if (!dp[i][j][k]) continue;
				        for (int p = j - 1; p <= j + 1; p++) {
							if (p < 0 || p > c) continue;
							if (k + c - p > n) continue;
							dp[i + 1][p][k + c - p] = true;
							pre[i + 1][p][k + c - p] = make_pair(j, k);
						}
					}
		    for (int i = 0; i <= c; i++) {
				if (!dp[r][i][n]) continue;
				cout << r << " " << c << endl;
				output(r, i, n, c);
				return 0;
			}
		}
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <assert.h>
using namespace std;

const int N = 110;
char s[110];
bool dp[6][25][110];
pair<int, int> pre[6][25][110];

void output(int r, int n, int m, int c) {
	if (!r) return;
	output(r - 1, pre[r][n][m].first, pre[r][n][m].second, c);
	for (int i = 1; i <= n; i++)
		printf("*");
	for (int j = pre[r][n][m].second + 1; j <=m; j++)
		printf("%c", s[j]);
	printf("\n");
}

int main()
{
	int n;
	scanf("%s", s + 1);
	n = strlen(s + 1);
	if (n <= 20) {
		printf("1 %d\n", n);
		cout << s + 1 << endl;
		return 0;
	}

    for (int r = 2; r <= 5; r++) {
		for (int c = 1; c <= 20; c++) {
			if (r * c < n) continue;
			for (int q = 0; q <= c; q++) {
				memset(dp, false, sizeof(dp));
				dp[1][q][c - q] = true;

			    for (int i = 1; i < r; i++)
			        for (int j = q; j <= q + 1; j++)
			            for (int k = 0; k <= n; k++) {
				            if (j > c || !dp[i][j][k]) continue;
				            for (int p = q; p <= q + 1; p++) {
							    if (p < 0 || p > c) continue;
							    if (k + c - p > n) continue;
							    dp[i + 1][p][k + c - p] = true;
							    pre[i + 1][p][k + c - p] = make_pair(j, k);
						    }
					    }
		        for (int i = q; i <= q + 1; i++) {
				    if (!dp[r][i][n]) continue;
				    cout << r << " " << c << endl;
				    output(r, i, n, c);
				    return 0;
			    }
		    }
		}
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <assert.h>
using namespace std;

const int N = 110;
char s[110];
bool dp[6][25][110];
pair<int, int> pre[6][25][110];

void output(int r, int n, int m, int c) {
	if (!r) return;
	output(r - 1, pre[r][n][m].first, pre[r][n][m].second, c);
	for (int i = 1; i <= n; i++)
		printf("*");
	for (int j = pre[r][n][m].second + 1; j <=m; j++)
		printf("%c", s[j]);
	printf("\n");
}

int main()
{
	int n;
	scanf("%s", s + 1);
	n = strlen(s + 1);
	if (n <= 20) {
		printf("1 %d\n", n);
		cout << s + 1 << endl;
		return 0;
	}

    for (int r = 2; r <= 5; r++) {
		for (int c = 1; c <= 20; c++) {
			if (r * c < n) continue;
			for (int q = 0; q <= c; q++) {
				memset(dp, false, sizeof(dp));
				dp[1][q][c - q] = true;

			    for (int i = 1; i < r; i++)
			        for (int j = q; j <= q + 1; j++)
			            for (int k = 0; k <= n; k++) {
				            if (!dp[i][j][k]) continue;
				            for (int p = j - 1; p <= j + 1; p++) {
							    if (p < 0 || p > c) continue;
							    if (k + c - p > n) continue;
							    dp[i + 1][p][k + c - p] = true;
							    pre[i + 1][p][k + c - p] = make_pair(j, k);
						    }
					    }
		        for (int i = q; i <= q + 1; i++) {
				    if (!dp[r][i][n]) continue;
				    cout << r << " " << c << endl;
				    output(r, i, n, c);
				    return 0;
			    }
		    }
		}
	}

	return 0;
}


/* Personalized Cup

- URL: https://codeforces.com/problemset/problem/1032/B
- Tags:
*/

#include <bits/stdc++.h>

using namespace std;

int main() {

  string s;
  int i = 1;
  int column;

  cin >> s;

  // cout << s.length();

  column = ceil(s.length() / (i*1.0f));
  while (column > 20){
    i++;
    column = ceil(s.length() / (i*1.0f));
  }

  int ast = (column * i) - s.length();
  int ast_print = 0;

  cout << i << ' ' << column;

  for(int j=0;j<=s.length();j++) {
    if (j % column == 0){cout<<endl;}
    if (ast > 0){cout << '*'; ast -= 1;ast_print += 1;}
    else{ cout << s[j-ast_print];}
  }

  // cout << s.length() ;
}


/* Personalized Cup

- URL: https://codeforces.com/problemset/problem/1032/B
- Tags:
*/

#include <bits/stdc++.h>

using namespace std;

int main() {

  string s;
  int i = 1;
  int column;

  cin >> s;

  // cout << s.length();

  column = ceil(s.length() / (i*1.0f));
  while (column > 20){
    i++;
    column = ceil(s.length() / (i*1.0f));
  }

  int ast = (column * i) - s.length();
  int astcp = ast;
  int ast_print = 0;

  cout << i << ' ' << column;

  for(int j=0;j<s.length()+astcp;j++) {
    if (j % column == 0){cout<<endl;}
    if (ast > 0){cout << '*'; ast -= 1;ast_print += 1;}
    else{ cout << s[j-ast_print];}
  }

  cout << endl;
}


/* Personalized Cup

- URL: https://codeforces.com/problemset/problem/1032/B
- Tags:
*/

#include <bits/stdc++.h>

using namespace std;

int main() {

  string s;
  int i = 1;
  int column;

  cin >> s;

  // cout << s.length();

  column = ceil(s.length() / (i*1.0f));
  while (column > 20){
    i++;
    column = ceil(s.length() / (i*1.0f));
  }

  int ast = (column * i) - s.length();
  int astcp = ast;
  int ast_print = 0;

  cout << i << ' ' << column << endl;

  int astperline = ceil(ast/(i*1.0f));

  bool linefill = false;

  for(int j=0;j<s.length()+astcp;j++) {
    if (j % column == 0){cout<<endl; linefill = false;}
    if ((ast > 0)&&(linefill == false)){
      int ammount = 0;
      while((ammount < astperline)&&(ast > 0)){
        cout << '*';
        ast -= 1;
        ast_print += 1;
        ammount += 1;
        linefill = true;
      }
    } else {
      cout << s[j-ast_print];
    }
  }

  cout << endl;
}


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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    string s;
    int r = 1, c, star, spr, printed = 0, cnt = 0, id = 0;

    cin >> s;
    while (1) {
        c = ceil(1.0*s.size()/r*1.0);
        if (c<=20) {
            star = c*r - s.size();
            spr = ceil((1.0*star/r*1.0));
            break;
        }
        r++;
    }
    cout << r << " " << c << '\n';
    for (int i = 0; i < r; i++) {
        printed = 0;
        for (int j = 0; j < c; j++) {
            if (id == s.size()) break;
            if (cnt != star && printed < spr) {
                cout << "*";
                cnt++;
                printed++;
            }
            else cout << s[id++];
        }
        cout << '\n';
    }
}


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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    string s;
    int r = 1, c, star, id = 0;

    cin >> s;
    while (1) {
        c = ceil(1.0*s.size()/r*1.0);
        if (c<=20) {
            star = c*r - s.size();
            break;
        }
        r++;
    }
    cout << r << " " << c << '\n';
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (id == s.size()) break;
            cout << s[id++];
        }
        if (i == r-1)
            for (int j = 0; j < star; j++)
                cout << "*";
        cout << '\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, rows, cols, j = 0, stars;
	string s;
	cin>>s;
	n = s.length();
	if(n<=20)
	{
		cout<<"1"<<" 20\n";
		cout<<s;
		return 0;
	}
	rows = 1 + (n-1)/20;
	cols = 1 + (n-1)/rows;
	stars = rows*cols - n;
	cout<<rows<<" "<<cols<<"\n";
	while(1)
	{
		if(stars > 0)
		{
			cout<<"*";
			stars--;
		}
		else
			cout<<s[j++];
		for(int i=1;i<cols;i++)
			cout<<s[j++];
		if(j==n)
			break;
		cout<<"\n";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, rows, cols, j = 0, stars;
	string s;
	cin>>s;
	n = s.length();
	if(n<=20)
	{
		cout<<"1 "<<n<<"\n";
		cout<<s;
		return 0;
	}
	rows = 1 + (n-1)/20;
	cols = 1 + (n-1)/rows;
	stars = rows*cols - n;
	cout<<rows<<" "<<cols<<"\n";
	while(1)
	{
		if(stars > 0)
		{
			cout<<"*";
			stars--;
		}
		else
			cout<<s[j++];
		for(int i=1;i<cols;i++)
			cout<<s[j++];
		if(j==n)
			break;
		cout<<"\n";
	}
	return 0;
}


#include <iostream>
using namespace std;
#include <algorithm>
#include <string>
int main()
{
	string str;
	cin>>str;
	for(int i=1;i<=5;i++)
	{
		if(str.size()/i<=20)
		{
			int start=0;
			int size=ceil(str.size()/float(i));
			int rem=str.size()%i;
			cout<<i<<" "<<size<<endl;
			if(i==1)
			{
				cout<<str;
				return 0 ;
			}
			/*
			for(int j=0;j<i;j++)
			{
				if(rem==0)
				{
					cout<<str.substr(start,size)<<endl;
					start+=size;
				}
				else if(rem%2==0)
				{
						cout<<str.substr(start,size)<<endl;
						start+=size;
				}
				else
				{
					cout<<str.substr(start,size-1)<<"*"<<endl;
					rem--;
					start+=size-1;
				}
			}*/

			if(rem==0)
			{
				for(int j=0;j<i;j++)
				{
					cout<<str.substr(start,size)<<endl;
					start+=size;
				}
				return 0;
			}
			for(int j=0;j<rem;j++)
			{
				cout<<str.substr(start,size)<<endl;
				start+=size;
			}
			if(rem!=i)
			{
				cout<<str.substr(start,size-(i-rem));
				for(int j=0;j<(i-rem);j++)
					cout<<"*";
			}
			return 0;
		}
	}
}


#include <iostream>
using namespace std;
#include <algorithm>
#include <string>
int main()
{
	string str;
	cin>>str;
	for(int i=1;i<=5;i++)
	{
		if(str.size()/i<=20)
		{
			int start=0;
			int size=ceil(str.size()/float(i));
			int rem=str.size()%i;
			cout<<i<<" "<<size<<endl;
			if(i==1)
			{
				cout<<str;
				return 0 ;
			}
			/*
			for(int j=0;j<i;j++)
			{
				if(rem==0)
				{
					cout<<str.substr(start,size)<<endl;
					start+=size;
				}
				else if(rem%2==0)
				{
						cout<<str.substr(start,size)<<endl;
						start+=size;
				}
				else
				{
					cout<<str.substr(start,size-1)<<"*"<<endl;
					rem--;
					start+=size-1;
				}
			}*/

			if(rem==0)
			{
				for(int j=0;j<i;j++)
				{
					cout<<str.substr(start,size)<<endl;
					start+=size;
				}
				return 0;
			}
			for(int j=0;j<rem;j++)
			{
				cout<<str.substr(start,size)<<endl;
				start+=size;
			}
			if(rem!=i)
			{
				for(int j=0;j<(i-rem);j++)
				{
					cout<<str.substr(start,size-1)<<"*"<<endl;
					start+=size-1;
				}
			}
			return 0;
		}
	}
}


#include <iostream>
using namespace std;
#include <algorithm>
#include <string>
int main()
{
	string str;
	cin>>str;
	for(int i=1;i<=5;i++)
	{
		if(str.size()/i<=20)
		{
			int start=0;
			int size=ceil(str.size()/float(i));
			int rem=str.size()%i;
			if(i==1)
			{
				cout<<str;
				return 0 ;
			}
			/*
			for(int j=0;j<i;j++)
			{
				if(rem==0)
				{
					cout<<str.substr(start,size)<<endl;
					start+=size;
				}
				else if(rem%2==0)
				{
						cout<<str.substr(start,size)<<endl;
						start+=size;
				}
				else
				{
					cout<<str.substr(start,size-1)<<"*"<<endl;
					rem--;
					start+=size-1;
				}
			}*/
			if(rem==0)
			{
				for(int j=0;j<i;j++)
				{
					cout<<str.substr(start,size)<<endl;
					start+=size;
				}
				return 0;
			}
			for(int j=0;j<rem;j++)
			{
				cout<<str.substr(start,size)<<endl;
				start+=size;
			}
			if(rem!=i)
			{
				cout<<str.substr(start,size-(i-rem));
				for(int j=0;j<(i-rem);j++)
					cout<<"*";
			}
			return 0;
		}
	}
}