#include <iostream>

using namespace std;

int main()
{
    long long int t,x,a,b;
    cin >> t;

    for (int i = 0; i < t; i++)
    {
        cin >> x;
        if (x%2 != 0)
        {
            cout << 1 << " " << x << endl;
        }
        else
        {
            cout << 2 << " " << (x-2)/2 << endl;
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
int num[maxn];
int h[maxn];
long long  dp[maxn][5];
int main()
{
	int q;
	scanf("%d",&q);
	while(q--)
	{
		int n;
		scanf("%d",&n);
		memset(dp,0x3f3f3f3f,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&h[i],&num[i]);
		}
		dp[1][0]=0;
		dp[1][1]=num[1];
		dp[1][2]=num[1]*2;
		for(int i=2;i<=n;i++)
		{
			for(int j=0;j<3;j++)
			{
				for(int k=0;k<3;k++)
				{
					if(h[i-1]+j!=h[i]+k)
					{
						dp[i][k]=min(dp[i][k],dp[i-1][j]+k*num[i]);
					}
				}
			}
		}
		printf("%I64d\n",min(dp[n][0],min(dp[n][1],dp[n][2])));
	}return 0;
 }


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+5;
ll dp[maxn][5];
const ll INF=0x3f3f3f3f3f3f3f;
pair<int,int>num[maxn];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&num[i].first,&num[i].second);
			dp[i][0]=dp[i][1]=dp[i][2]=INF;
		}
		dp[1][0]=0;
		dp[1][1]=num[1].second;
		dp[1][2]=num[1].second*2;
		for(int i=2;i<=n;i++)
		{
			for(int j=0;j<=2;j++)
			{
				for(int k=0;k<=2;k++)
				{
					if(num[i-1].first+j!=num[i].first+k)
					{
						dp[i][k]=min(dp[i][k],dp[i-1][j]+num[i].second*k);
					}
				}
			}

		}
		printf("%I64d\n",min(dp[n][0],min(dp[n][1],dp[n][2])));
	}
 }


#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+5;
int num[maxn];
int h[maxn];
long long  dp[maxn][5];
int main()
{
	int q;
	scanf("%d",&q);
	while(q--)
	{
		int n;
		scanf("%d",&n);
		memset(dp,0x3f3f3f3f3f3f3f3f,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&h[i],&num[i]);

		}
		dp[1][0]=0;
		dp[1][1]=num[1];
		dp[1][2]=num[1]*2;
		for(int i=2;i<=n;i++)
		{
			for(int j=0;j<3;j++)
			{
				for(int k=0;k<3;k++)
				{
					if(h[i-1]+j!=h[i]+k)
					{
						dp[i][k]=min(dp[i][k],dp[i-1][j]+k*num[i]);
					}
				}
			}
		}
		printf("%I64d\n",min(dp[n][0],min(dp[n][1],dp[n][2])));
	}return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define ull unsigned long long

int a[300005], b[300005];
ull dp[300005][3];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int q, n;

	cin >> q;
	while (cin >> n) {
		for (int i = 0;i < n;i++) {
			cin >> a[i] >> b[i];
		}
		dp[0][0] = 0;
		dp[0][1] = b[0];
		dp[0][2] = b[0] + b[0];
		for (int i = 1;i < n;i++) {
			int t;
			for (int j = 0;j < 3;j++) {
				t = a[i] + j;
				dp[i][j] = 2000000000000000000;
				for (int k = 0;k < 3;k++) {
					if (t != (a[i-1]+k)) {
						dp[i][j] = min(dp[i][j], dp[i-1][k] + (ull)b[i]*(ull)j);
					}
				}
			}
		}
		cout << min(dp[n-1][0], min(dp[n-1][1], dp[n-1][2])) << endl;
	}
	return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

#define ull unsigned long long

int a[300005], b[300005];
ull dp[300005][3];

int main() {
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    // cout.tie(nullptr);
	int q, n;

	cin >> q;
	while (cin >> n) {
		for (int i = 0;i < n;i++) {
			cin >> a[i] >> b[i];
		}
		dp[0][0] = 0;
		dp[0][1] = b[0];
		dp[0][2] = b[0] + b[0];
		for (int i = 1;i < n;i++) {
			int t;
			for (int j = 0;j < 3;j++) {
				t = a[i] + j;
				dp[i][j] = 2000000000000000000;
				for (int k = 0;k < 3;k++) {
					if (t != (a[i-1]+k)) {
						dp[i][j] = min(dp[i][j], dp[i-1][k] + (ull)b[i]*(ull)j);
					}
				}
			}
		}
		cout << min(dp[n-1][0], min(dp[n-1][1], dp[n-1][2])) << endl;
	}
	return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

#define ull unsigned long long

int a[300005], b[300005];
ull dp[300005][3];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int q, n;

	cin >> q;
	while (cin >> n) {
		for (int i = 0;i < n;i++) {
			cin >> a[i] >> b[i];
		}
		dp[0][0] = 0;
		dp[0][1] = b[0];
		dp[0][2] = b[0] + b[0];
		for (int i = 1;i < n;i++) {
			int t;
			for (int j = 0;j < 3;j++) {
				t = a[i] + j;
				dp[i][j] = 2000000000000000000;
				for (int k = 0;k < 3;k++) {
					if (t != (a[i-1]+k)) {
						dp[i][j] = min(dp[i][j], dp[i-1][k] + (ull)b[i]*(ull)j);
					}
				}
			}
		}
		cout << min(dp[n-1][0], min(dp[n-1][1], dp[n-1][2])) << endl;
	}
	return 0;
}


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

int a[300005], b[300005];
int n;
ll dp[300005][4];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        if(n == 0){
            cout << 0 << endl;
            continue;
        }

        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < 4; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < 4; ++j){
                    dp[i][j] = LONG_LONG_MAX;
                    for(int k = 0; k < 4; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = LONG_LONG_MAX;
        for(int i = 0; i < 4; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+5;

ll a[3000050], b[3000050];
int n;
ll dp[3000050][5];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        if(n == 1){
            cout << 0 << endl;
            continue;
        }

        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < 4; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < 4; ++j){
                    dp[i][j] = INF;
                    for(int k = 0; k < 4; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = INF;
        for(int i = 0; i < 4; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+5;

ll a[3000050], b[3000050];
int n;
ll dp[3000050][5];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < 4; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < 4; ++j){
                    dp[i][j] = INF;
                    for(int k = 0; k < 4; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = INF;
        for(int i = 0; i < 4; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+5;
const int size = 4;

ll a[300005], b[300005];
int n;
ll dp[300005][size];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        if(n == 1){
            cout << 0 << endl;
            continue;
        }

        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < size; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < size; ++j){
                    dp[i][j] = INF;
                    for(int k = 0; k < size; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = INF;
        for(int i = 0; i < size; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+5;
const int size = 4;

ll a[300050], b[300005];
int n;
ll dp[300050][size+1];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        if(n == 1){
            cout << 0 << endl;
            continue;
        }

        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < size; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < size; ++j){
                    dp[i][j] = INF;
                    for(int k = 0; k < size; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = INF;
        for(int i = 0; i < size; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+5;

ll a[3000050], b[3000050];
int n;
ll dp[3000050][5];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < 4; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < 4; ++j){
                    dp[i][j] = INF;
                    for(int k = 0; k < 4; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = INF;
        for(int i = 0; i < 4; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;}


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

int a[300005], b[300005];
int n;
ll dp[300005][4];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        if(n == 0){
            cout << 0 << endl;
            continue;
        }

        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < 4; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < 4; ++j){
                    dp[i][j] = 1<<63;
                    for(int k = 0; k < 4; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = 1<<63;
        for(int i = 0; i < 4; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18+5;

int a[300005], b[300005];
int n;
ll dp[300005][4];

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        if(n == 0){
            cout << 0 << endl;
            continue;
        }

        for(int i = 0; i < n; ++i){
            cin >> a[i] >> b[i];
            if(i == 0){
                for(int j = 0; j < 4; ++j)dp[0][j] = b[0]*j;
                continue;
            }
            else {
                for(int j = 0; j < 4; ++j){
                    dp[i][j] = INF;
                    for(int k = 0; k < 4; ++k){
                        if(a[i]+j != a[i-1]+k)dp[i][j] = min(dp[i][j], dp[i-1][k]+b[i]*j);
                    }
                }
            }

        }
        ll minn = INF;
        for(int i = 0; i < 4; ++i){
            minn = min(dp[n-1][i], minn);
        }
        cout << minn << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef unsigned long long int ll;
const int N=3e5+5;
int t,n,v[N];
ll ans;
pair < int, ll > a[N];
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ans=0;
        memset(v,0,sizeof v);
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i].f>>a[i].s;
        a[n]= mp(a[n-1].f+3,0);
        for(int i=0;i<n-1;i++){
            if(a[i].f==a[i+1].f){
                ll op1=0,op2=0;
                for(int j=i;j<n&&a[j].f==a[i].f;j+=2){

                    op1+=a[j].s * 1ll * (j==i&&i?(a[j-1].f==a[i].f+1?2:1):1) *(a[j+1].f==a[i].f+1?2:1) ;
                     if( j==i && i>1 && (a[j-1].f==a[i].f+1) && v[j-1] )
                            op1-= max(a[j].s,a[j-1].s);
                }
                for(int j=i+1;j<n&&a[j].f==a[i].f;j+=2)
                    op2+=a[j].s * 1ll * (a[j+1].f==a[i].f+1?2:1) ;
                int tmp=a[i].f,tmp2=i;
              // cout<<op1<<" "<<op2<<'\n';
                while(a[tmp2].f==tmp) tmp2++;
                tmp2--;
                if(op2<op1)
                     for(int j=i+1;j<n&&a[j].f==a[i].f;j+=2){
                        a[j].f+= (a[j+1].f==a[i].f+1?2:1) ;
                        v[j]=1;
                     }
                else{
                    for(int j=i;j<n&&a[j].f==a[i].f;j+=2){
                        v[j]=1;
                        a[j].f+=(j==i&&i?(a[j-1].f==a[i].f+1?2:1):1) * (a[j+1].f==a[i].f+1?2:1) ;
                    }
                }
                i=tmp2;
                ans+= min(op1,op2);
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef long long int ll;
int t,n,a,b,lst;
ll ans;
priority_queue < pair<int,int> > q;
map < int, int > m;
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ans=0; lst=1;
        m.clear();
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a>>b;
            q.push(mp(b,-a));
        }
        while(q.size()){
            pair<int,int> tmp= q.top();
            q.pop();
            if(!m[-tmp.s]) m[-tmp.s]=-tmp.s;
            ans+= (m[-tmp.s]+tmp.s) * 1ll * tmp.f;
            if(m[-tmp.s+1]) m[-tmp.s]=m[-tmp.s+1];
            else
                m[-tmp.s]= -tmp.s+1;
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef unsigned long long int ll;
const int N=3e5+5;
int t,n;
ll ans;
pair < int, ll > a[N];
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ans=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i].f>>a[i].s;
        a[n]= mp(a[n-1].f+3,0);
        for(int i=0;i<n-1;i++){
            if(a[i].f==a[i+1].f){
                ll op1=0,op2=0;
                for(int j=i;j<n&&a[j].f==a[i].f;j+=2)
                    op1+=a[j].s * 1ll * (j==i&&i?(a[j-1].f==a[i].f+1?2:1):1) *(a[j+1].f==a[i].f+1?2:1) ;
                for(int j=i+1;j<n&&a[j].f==a[i].f;j+=2)
                    op2+=a[j].s * 1ll * (a[j+1].f==a[i].f+1?2:1) ;
                int tmp=a[i].f,tmp2=i;
               // cout<<op1<<" "<<op2<<'\n';
                while(a[tmp2].f==tmp) tmp2++;
                tmp2--;
                if(op2<op1)
                     for(int j=i+1;j<n&&a[j].f==a[i].f;j+=2)
                        a[j].f+= (a[j+1].f==a[i].f+1?2:1) ;
                else{
                    for(int j=i;j<n&&a[j].f==a[i].f;j+=2)
                        a[j].f+=(j==i&&i?(a[j-1].f==a[i].f+1?2:1):1) * (a[j+1].f==a[i].f+1?2:1) ;
                }
                i=tmp2;
                ans+= min(op1,op2);
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef unsigned long long int ll;
const int N=3e5+5;
int t,n;
ll ans;
pair < int, ll > a[N];
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ans=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i].f>>a[i].s;
        a[n]= mp(a[n-1].f+3,0);
        for(int i=0;i<n-1;i++){
            if(a[i].f==a[i+1].f){
                if(a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) + (a[i+2].f==a[i].f?a[i+2].s*1ll * (a[i+3].f==a[i].f+1?2:1): 0) < a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ){
                    ans+= a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) ;
                    a[i].f+= (i?(a[i-1].f==a[i].f+1?2:1):1);
                }
                else{
                    ans+= a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ;
                    a[i+1].f+= (a[i+2].f==a[i].f+1?2:1);
                }
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef unsigned long long int ll;
const int N=3e5+5;
int t,n;
ll ans;
pair < int, ll > a[N];
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ans=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i].f>>a[i].s;
        a[n]= mp(a[n-1].f+3,0);
        for(int i=0;i<n-1;i++){
            if(a[i].f==a[i+1].f){
                if(a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) < a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ){
                    ans+= a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) ;
                    a[i].f+= (i?(a[i-1].f==a[i].f+1?2:1):1);
                }
                else{
                    ans+= a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ;
                    a[i+1].f+= (a[i+2].f==a[i].f+1?2:1);
                }
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef long long int ll;
const int N=3e5+5;
int t,n;
ll ans;
pair < int,int > a[N];
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ans=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i].f>>a[i].s;
        a[n]= mp(a[n-1].f+3,0);
        for(int i=0;i<n-1;i++){
            if(a[i].f==a[i+1].f){
                if(a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) < a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ){
                    ans+= a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) ;
                    a[i].f+= (i?(a[i-1].f==a[i].f+1?2:1):1);
                }
                else{
                    ans+= a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ;
                    a[i+1].f+= (a[i+2].f==a[i].f+1?2:1);
                }
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef long long int ll;
const int N=3e5+1;
const ll MAX= 1e18;
int t,n;
ll memo [N][3];
pair < int, int > a[N];
ll dfs(int ind, int ad){
    if(ind==n) return 0;
    if(~memo[ind][ad])
        return memo[ind][ad];
    ll ret=MAX;
    for(int i=0;i<3;i++){
        if(ind&&a[ind].f+i==a[ind-1].f+ad)
            continue;
        ret=min(ret,dfs(ind+1,i)+a[ind].s*1ll*i);
    }
    return memo[ind][ad]=ret;
}
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&a[i].f,&a[i].s);
            memo[i][0]=memo[i][1]=memo[i][2]=-1;
        }
        cout<<dfs(0,0)<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef unsigned long long int ll;
const int N=3e5+5;
int t,n;
ll ans;
pair < int, ll > a[N];
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        ans=0;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i].f>>a[i].s;
        a[n]= mp(a[n-1].f+3,0);
        for(int i=0;i<n-1;i++){
            if(a[i].f==a[i+1].f){
                if(a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) + (a[i+2].f==a[i].f?a[i+2].s*1ll * (a[i+3].f==a[i].f+1?2:1): 0) <= a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ){
                    ans+= a[i].s * 1ll * (i?(a[i-1].f==a[i].f+1?2:1):1) ;
                    a[i].f+= (i?(a[i-1].f==a[i].f+1?2:1):1);
                }
                else{
                    ans+= a[i+1].s * 1ll * (a[i+2].f==a[i].f+1?2:1) ;
                    a[i+1].f+= (a[i+2].f==a[i].f+1?2:1);
                }
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef long long int ll;
const int N=3e5+5;
const ll MAX= 1e18;
int t,n;
ll memo [N][4];
pair < int, int > a[N];
ll dfs(int ind, int ad){
    if(ind==n) return 0;
    if(~memo[ind][ad])
        return memo[ind][ad];
    ll ret=MAX;
    for(int i=0;i<3;i++){
        if(ind&&a[ind].f+i==a[ind-1].f+ad)
            continue;
        ret=min(ret,dfs(ind+1,i)+a[ind].s*1ll*i);
    }
    return memo[ind][ad]=ret;
}
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    cin>>t;
    while(t--){
        memset(memo,-1,sizeof memo);
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i].f>>a[i].s;
        cout<<dfs(0,0)<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define mp make_pair
#define pb push_back
typedef long long int ll;
const int N=3e5+1;
const ll MAX= 1e18;
int t,n;
ll memo [N][3];
pair < int, int > a[N];
ll dfs(int ind, int ad){
    if(ind==n) return 0;
    if(~memo[ind][ad])
        return memo[ind][ad];
    ll ret=MAX;
    for(int i=0;i<3;i++){
        if(ind&&a[ind].f+i==a[ind-1].f+ad)
            continue;
        ret=min(ret,dfs(ind+1,i)+a[ind].s*1ll*i);
    }
    return memo[ind][ad]=ret;
}
int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(false);
    scanf("%d",&t);
    while(t--){
        memset(memo,-1,sizeof memo);
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d",&a[i].f,&a[i].s);
        cout<<dfs(0,0)<<'\n';
    }
    return 0;
}


#include<iostream>
#define ll long long
using namespace std;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll t;
  scanf("%lld",&t);
  for(ll h=0;h<t;h++){
  ll n;
  ll dp[3][300005];
  ll a[300005],b[300005];
  const ll inf=0x3f3f3f3f3f3f3f3f;
  scanf("%lld",&n);
  for(int i=0;i<n;i++)
  {
    scanf("%lld",&a[i]);
    scanf("%lld",&b[i]);
  }
  for(ll i=1;i<n;i++)
  {
  	for(int j=0;j<3;j++){
  		dp[j][i]=inf;
	  }
  }
  dp[0][0]=0;
  dp[1][0]=b[0];
  dp[2][0]=b[0]*2;
  for(ll i=1;i<n;i++)
  {
    for(int x=0;x<3;x++)
    {
      for(int y=0;y<3;y++)
        if(a[i-1]+y!=a[i]+x)
        {
          dp[x][i]=min(dp[x][i],dp[y][i-1]+x*b[i]);
        }
    }
  }

  cout<<min(dp[0][n-1],min(dp[1][n-1],dp[2][n-1]))<<endl;
}
}


#include<iostream>
#define ll long long
using namespace std;
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll t;
  cin>>t;
  for(ll h=0;h<t;h++){
  ll n;
  ll dp[3][300005];
  ll a[300005],b[300005];
  const ll inf=0x3f3f3f3f3f3f3f3f;
  scanf("%ll",&n);
  for(int i=0;i<n;i++)
  {
    scanf("%ll",&a[i]);
    scanf("%ll",&b[i]);
  }
  for(ll i=1;i<n;i++)
  {
  	for(int j=0;j<3;j++){
  		dp[j][i]=inf;
	  }
  }
  dp[0][0]=0;
  dp[1][0]=b[0];
  dp[2][0]=b[0]*2;
  for(ll i=1;i<n;i++)
  {
    for(int x=0;x<3;x++)
    {
      for(int y=0;y<3;y++)
        if(a[i-1]+y!=a[i]+x)
        {
          dp[x][i]=min(dp[x][i],dp[y][i-1]+x*b[i]);
        }
    }
  }

  cout<<min(dp[0][n-1],min(dp[1][n-1],dp[2][n-1]))<<endl;
}
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_iprice[300100];
long long dp_itprice[300100];
long long dp_oprice[300100];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
			dp_iprice[i] = 0;
			dp_itprice[i] = 0;
			dp_oprice[i] = 0;
		}

		for(long long i = 0; i <n; i++)
		{
			if(n == 0)
			{
				dp_itprice[i] = price[i]*2;
				dp_iprice[i] = price[i];
				dp_oprice[i]  = 0;
			}
			else
			{
				long long increase = 99999999999;
				long long itncrease = 99999999999;
				long long oprice = 99999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_itprice[i  - 1]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_itprice[i - 1];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_iprice[i - 1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_iprice[i - 1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i  - 1]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_itprice[i  - 1]  < increase )
				{
					increase =   price[i] * 2 +  dp_itprice[i - 1];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_iprice[i - 1] < increase )
				{
					increase =  price[i]  + dp_iprice[i - 1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1];
				}

				if(height[i] != height[i - 1] +2  && dp_itprice[i - 1] < oprice )
				{
					oprice =  dp_itprice[i - 1];
				}
				if(height[i]  != height[i - 1] + 1 && dp_iprice[i - 1] < oprice )
				{
					oprice =  dp_iprice[i - 1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1]  < oprice )
				{
					oprice =  dp_oprice[i - 1];
				}

				dp_itprice[i] = itncrease;
				dp_iprice[i] = increase;
				dp_oprice[i] = oprice;

			}
		}

		long long answer = 	dp_iprice[n - 1] > dp_oprice[n - 1] ? dp_oprice[n - 1] : dp_iprice[ n - 1];
		cout << answer << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(i == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 9999999999999;
				long long itncrease = 9999999999999;
				long long oprice = 9999999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 9999999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(i == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 9999999999999;
				long long itncrease = 9999999999999;
				long long oprice = 9999999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 9999999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(n == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 99999999999;
				long long itncrease = 99999999999;
				long long oprice = 99999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 99999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(i == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 99999999999;
				long long itncrease = 99999999999;
				long long oprice = 99999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 99999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(n == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 99999999999;
				long long itncrease = 99999999999;
				long long oprice = 99999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 99999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_iprice[300100];
long long dp_itprice[300100];
long long dp_oprice[300100];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
			dp_iprice[i] = 0;
			dp_itprice[i] = 0;
			dp_oprice[i] = 0;
		}

		for(long long i = 0; i <n; i++)
		{
			if(n == 0)
			{
				dp_itprice[i] = price[i]*2;
				dp_iprice[i] = price[i];
				dp_oprice[i]  = 0;
			}
			else
			{
				long long increase = 99999999999;
				long long itncrease = 99999999999;
				long long oprice = 99999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_itprice[i - 1];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_iprice[i - 1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_iprice[i - 1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i  - 1]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1];
				}

				if(height[i] + 12 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1]  < increase )
				{
					increase =   price[i] * 2 +  dp_itprice[i - 1];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_iprice[i - 1] < increase )
				{
					increase =  price[i]  + dp_iprice[i - 1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1];
				}

				if(height[i] != height[i - 1] +2  && dp_iprice[i - 1] < oprice )
				{
					oprice =  dp_itprice[i - 1];
				}
				if(height[i]  != height[i - 1] + 1 && dp_iprice[i - 1] < oprice )
				{
					oprice =  dp_iprice[i - 1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1]  < oprice )
				{
					oprice =  dp_oprice[i - 1];
				}

				dp_itprice[i] = itncrease;
				dp_iprice[i] = increase;
				dp_oprice[i] = oprice;

			}
		}

		long long answer = 	dp_iprice[n - 1] > dp_oprice[n - 1] ? dp_oprice[n - 1] : dp_iprice[ n - 1];
		cout << answer << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(i == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 999999999999999;
				long long itncrease = 999999999999999;
				long long oprice = 999999999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 999999999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_iprice[300100];
long long dp_itprice[300100];
long long dp_oprice[300100];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
			dp_iprice[i] = 0;
			dp_itprice[i] = 0;
			dp_oprice[i] = 0;
		}

		for(long long i = 0; i <n; i++)
		{
			if(n == 0)
			{
				dp_itprice[i] = price[i]*2;
				dp_iprice[i] = price[i];
				dp_oprice[i]  = 0;
			}
			else
			{
				long long increase = 99999999999;
				long long itncrease = 99999999999;
				long long oprice = 99999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_itprice[i - 1];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_iprice[i - 1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_iprice[i - 1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i  - 1]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1]  < increase )
				{
					increase =   price[i] * 2 +  dp_itprice[i - 1];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_iprice[i - 1] < increase )
				{
					increase =  price[i]  + dp_iprice[i - 1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1];
				}

				if(height[i] != height[i - 1] +2  && dp_iprice[i - 1] < oprice )
				{
					oprice =  dp_itprice[i - 1];
				}
				if(height[i]  != height[i - 1] + 1 && dp_iprice[i - 1] < oprice )
				{
					oprice =  dp_iprice[i - 1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1]  < oprice )
				{
					oprice =  dp_oprice[i - 1];
				}

				dp_itprice[i] = itncrease;
				dp_iprice[i] = increase;
				dp_oprice[i] = oprice;

			}
		}

		long long answer = 	dp_iprice[n - 1] > dp_oprice[n - 1] ? dp_oprice[n - 1] : dp_iprice[ n - 1];
		cout << answer << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(i == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 99999999999999;
				long long itncrease = 99999999999999;
				long long oprice = 99999999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 99999999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(n == 0)
			{
				dp_oprice[i][0] = price[i]*2;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = 0;
			}
			else
			{
				long long increase = 99999999999;
				long long itncrease = 99999999999;
				long long oprice = 99999999999;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 99999999999;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <iostream>

using namespace std;

long long height[300100];
long long price[300100];
long long dp_oprice[300100][3];

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

	long long q;
	cin >> q;

	while(q--)
	{
		long long  n;
		cin >> n;
		for(long long i = 0; i <n; i++)
		{
			long long cinheight , cinprice;
			cin >> cinheight >> cinprice;
			height[i] = cinheight;
			price[i] = cinprice;
		}

		for(long long i = 0; i <n; i++)
		{
			if(i == 0)
			{
				dp_oprice[i][0] = 0;
				dp_oprice[i][1] = price[i];
				dp_oprice[i][2]  = price[i]*2;
			}
			else
			{
				long long increase = 250000000000000;
				long long itncrease = 250000000000000;
				long long oprice = 250000000000000;

				if(height[i] + 2 != height[i - 1] + 2 && price[i]*2 + dp_oprice[i  - 1][2]  < itncrease )
				{
					itncrease =   price[i] * 2 +  dp_oprice[i - 1][2];
				}
				if(height[i] + 2 != height[i - 1] + 1 && price[i]*2 + dp_oprice[i - 1][1]  <   itncrease )
				{
					itncrease =  price[i]*2   + dp_oprice[i - 1][1] ;
				}
				if(height[i] + 2 != height[i - 1]  && price[i]*2 + dp_oprice[i - 1][0]   <   itncrease )
				{
					itncrease =   price[i]*2  +  dp_oprice[i - 1][0];
				}

				if(height[i] + 1 != height[i - 1] + 2 && price[i] + dp_oprice[i  - 1][2]  < increase )
				{
					increase =   price[i] +  dp_oprice[i - 1][2];
				}
				if(height[i] + 1 != height[i - 1] + 1 && price[i] + dp_oprice[i - 1][1] < increase )
				{
					increase =  price[i]  +  dp_oprice[i - 1][1] ;
				}
				if(height[i] + 1 != height[i - 1]  && price[i] + dp_oprice[i  - 1][0]  < increase )
				{
					increase =   price[i]  +  dp_oprice[i - 1][0];
				}

				if(height[i] != height[i - 1] +2  && dp_oprice[i - 1][2] < oprice )
				{
					oprice =  dp_oprice[i - 1][2];
				}
				if(height[i]  != height[i - 1] + 1 && dp_oprice[i - 1][1] < oprice )
				{
					oprice =  dp_oprice[i - 1][1] ;
				}
				if(height[i] != height[i - 1]  && dp_oprice[i - 1][0]  < oprice )
				{
					oprice =  dp_oprice[i - 1][0];
				}

				dp_oprice[i][2] = itncrease;
				dp_oprice[i][1] = increase;
				dp_oprice[i][0] = oprice;

			}
		}

		long long min = 250000000000000;
		for(int i = 0; i < 3;i++)
		{
			if(min > dp_oprice[n- 1][i])
			{
				min = dp_oprice[n- 1][i];
			}
		}

		cout << min << endl;

	}

	return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
typedef long long ll ;
const int N= 3e5 + 10 ;
const int INF = 1e18 ;
ll a[N] , b[N] ;
ll dp[N][3] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		ll n ;
		scanf ("%lld",&n) ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] = INF ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (ll i = 2 ; i <= n ; ++ i){
			for (ll j = 0 ; j < 3 ; ++ j){	//枚举i-1的增长量
				for (ll k = 0 ; k < 3 ; ++ k){	//枚举i的增长量
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][0],min(dp[n][1],dp[n][2]))) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
const int N= 3e5 + 10 ;
const int INF = 0x3f3f3f3f ;
typedef long long ll ;
ll a[N] , b[N] , dp[N][2] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		int n ;
		scanf ("%d",&n) ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0]=INF, dp[i][1] =INF, dp[i][2] = INF ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (int i = 2 ; i <= n ; ++ i){
			for (int j = 0 ; j < 3 ; ++ j){
				for (int k = 0 ; k < 3 ; ++ k){
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][2],min(dp[n][1],dp[n][0]))) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
typedef long long ll ;
const int N= 3e5 + 10 ;
const int INF = 2e18 ;
ll a[N] , b[N] , dp[N][2] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		int n ;
		scanf ("%d",&n) ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0]=INF, dp[i][1] =INF, dp[i][2] = INF ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (int i = 2 ; i <= n ; ++ i){
			for (int j = 0 ; j < 3 ; ++ j){
				for (int k = 0 ; k < 3 ; ++ k){
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][2],min(dp[n][1],dp[n][0]))) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std ;
typedef long long ll ;
const int N= 3e5 + 10 ;
const int INF = 1e18 ;
ll a[N] , b[N] ;
ll dp[N][3] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		ll n ;
		scanf ("%lld",&n) ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] = INF ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (ll i = 2 ; i <= n ; ++ i){
			for (ll j = 0 ; j < 3 ; ++ j){	//枚举i-1的增长量
				for (ll k = 0 ; k < 3 ; ++ k){	//枚举i的增长量
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		cout << min(dp[n][0],min(dp[n][1],dp[n][2])) << endl  ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
const int N= 3e5 + 10 ;
const int INF = 0x3f3f3f3f ;
typedef long long ll ;
ll a[N] , b[N] , dp[N][2] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		int n ;
		scanf ("%d",&n) ;
		ll cost = 0 ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (int i = 2 ; i <= n ; ++ i){
			for (int j = 0 ; j < 3 ; ++ j){
				for (int k = 0 ; k < 3 ; ++ k){
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][1],dp[n][0])) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
const int N= 3e5 + 10 ;
const int INF = 0x3f3f3f3f ;
typedef long long ll ;
ll a[N] , b[N] , dp[N][2] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		int n ;
		scanf ("%d",&n) ;
		ll cost = 0 ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (int i = 2 ; i <= n ; ++ i){
			for (int j = 0 ; j < 3 ; ++ j){
				for (int k = 0 ; k < 3 ; ++ k){
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k)
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][1],dp[n][0])) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
typedef long long ll ;
const int N= 3e5 + 10 ;
const int INF = 1e18 ;
ll a[N] , b[N] , dp[N][3] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		int n ;
		scanf ("%d",&n) ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] = INF ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (int i = 2 ; i <= n ; ++ i){
			for (int j = 0 ; j < 3 ; ++ j){
				for (int k = 0 ; k < 3 ; ++ k){
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][2],min(dp[n][1],dp[n][0]))) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
typedef long long ll ;
const int N= 3e5 + 10 ;
const ll INF = 1e18 ;
ll a[N] , b[N] ;
ll dp[N][3] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		ll n ;
		scanf ("%lld",&n) ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] = INF ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (ll i = 2 ; i <= n ; ++ i){
			for (ll j = 0 ; j < 3 ; ++ j){	//枚举i-1的增长量
				for (ll k = 0 ; k < 3 ; ++ k){	//枚举i的增长量
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][0],min(dp[n][1],dp[n][2]))) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
const int N= 3e5 + 10 ;
const int INF = 0x3f3f3f3f ;
typedef long long ll ;
ll a[N] , b[N] , dp[N][2] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		int n ;
		scanf ("%d",&n) ;
		ll cost = 0 ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] = INF ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (int i = 2 ; i <= n ; ++ i){
			for (int j = 0 ; j < 3 ; ++ j){
				for (int k = 0 ; k < 3 ; ++ k){
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][2],min(dp[n][1],dp[n][0]))) ;
	}
	return 0 ;
}


#include <cstdio>
#include <algorithm>
using namespace std ;
const int N= 3e5 + 10 ;
const int INF = 0x3f3f3f3f ;
typedef long long ll ;
ll a[N] , b[N] , dp[N][2] ;
int main(){
	int t ;
	scanf ("%d",&t) ;
	while(t --){
		int n ;
		scanf ("%d",&n) ;
		ll cost = 0 ;
		for (int i = 1 ; i <= n ; ++ i){
			scanf ("%lld%lld",&a[i],&b[i]) ;
			dp[i][0] = dp[i][1] = dp[i][2] ;
		}
		dp[1][0] = 0 , dp[1][1] = b[1] , dp[1][2] = 2*b[1] ;
		for (int i = 2 ; i <= n ; ++ i){
			for (int j = 0 ; j < 3 ; ++ j){
				for (int k = 0 ; k < 3 ; ++ k){
					if (a[i-1]+j != a[i]+k){
						dp[i][k] = min(dp[i][k],dp[i-1][j]+b[i]*k) ;
					}
				}
			}
		}
		printf ("%lld\n",min(dp[n][2],min(dp[n][1],dp[n][0]))) ;
	}
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
#define ll long long
ll maxval = 1e18+7;
ll casio[300005][105],cs=0;
ll dp[300005][105],a[300005],b[300005];
ll n;
ll call(ll pos,ll inc)
{
	if(pos==n+1)return 0;
	if(casio[pos][inc]==cs)return dp[pos][inc];
	casio[pos][inc] = cs;
	ll ret = maxval;
	ll prev = a[pos-1]+inc;
	for(ll j = 0;j<=100;j++)
	{
		if(prev!=a[pos]+j)
		{
			ret = min(ret,call(pos+1,j)+(j*b[pos]));
		}
	}
	return dp[pos][inc] = ret;
}

int main()
{

	ll q;
	cin >> q;
	while(q--)
	{
		cs++;
		cin >> n;
		for(ll i=1;i<=n;i++)
		{
			cin >> a[i] >> b[i];
		}
		cout << call(1,0) << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
#define ll long long
ll maxval = 1e18+7;
ll casio[300005][10],cs=0;
ll dp[300005][10],a[300005],b[300005];
ll n;
ll call(ll pos,ll inc)
{
	if(pos==n+1)return 0;
	if(casio[pos][inc]==cs)return dp[pos][inc];
	casio[pos][inc] = cs;
	ll ret = maxval;
	ll prev = a[pos-1]+inc;
	if(prev!=a[pos])
		ret = min(ret,call(pos+1,0));
	if(prev!=a[pos]+1)
	ret = min(ret,call(pos+1,1)+b[pos]);
	if(prev!=a[pos]+1)
	ret = min(ret,call(pos+1,2)+(2*b[pos]));
	if(prev!=a[pos]+3)
	ret = min(ret,call(pos+1,3)+(3*b[pos]));
	return dp[pos][inc] = ret;
}

int main()
{

	ll q;
	cin >> q;
	while(q--)
	{
		cs++;
		cin >> n;
		for(ll i=1;i<=n;i++)
		{
			cin >> a[i] >> b[i];
		}
		cout << call(1,0) << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
#define ll long long
ll maxval = 1e18+7;
ll casio[300005][12],cs=0;
ll dp[300005][12],a[300005],b[300005];
ll n;
ll call(ll pos,ll inc)
{
    if(pos==n+1)return 0;
    if(casio[pos][inc]==cs)return dp[pos][inc];
    casio[pos][inc] = cs;
    ll ret = maxval;
    ll prev = a[pos-1]+inc;
    for(ll j = 0;j<=10;j++)
    {
        if(prev!=a[pos]+j)
        {
            ret = min(ret,call(pos+1,j)+(j*b[pos]));
        }
    }
    return dp[pos][inc] = ret;
}

int main()
{
    ios_ase::sync_with_stdio(false);
    ll q;
    cin >> q;
    while(q--)
    {
        cs++;
        cin >> n;
        for(ll i=1;i<=n;i++)
        {
            cin >> a[i] >> b[i];
        }
        cout << call(1,0) << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
#define ll long long
ll maxval = 1e18+7;
ll casio[300005][12],cs=0;
ll dp[300005][12],a[300005],b[300005];
ll n;
ll call(ll pos,ll inc)
{
    if(pos==n+1)return 0;
    if(casio[pos][inc]==cs)return dp[pos][inc];
    casio[pos][inc] = cs;
    ll ret = maxval;
    ll prev = a[pos-1]+inc;
    for(ll j = 0;j<=10;j++)
    {
        if(prev!=a[pos]+j)
        {
            ret = min(ret,call(pos+1,j)+(j*b[pos]));
        }
    }
    return dp[pos][inc] = ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    ll q;
    cin >> q;
    while(q--)
    {
        cs++;
        cin >> n;
        for(ll i=1;i<=n;i++)
        {
            cin >> a[i] >> b[i];
        }
        cout << call(1,0) << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
#define ll long long
ll maxval = 1e18+7;
ll casio[300005][12],cs=0;
ll dp[300005][12],a[300005],b[300005];
ll n;
ll call(ll pos,ll inc)
{
	if(pos==n+1)return 0;
	if(casio[pos][inc]==cs)return dp[pos][inc];
	casio[pos][inc] = cs;
	ll ret = maxval;
	ll prev = a[pos-1]+inc;
	for(ll j = 0;j<=3;j++)
	{
		if(prev!=a[pos]+j)
		{
			ret = min(ret,call(pos+1,j)+(j*b[pos]));
		}
	}
	return dp[pos][inc] = ret;
}

int main()
{

	ll q;
	cin >> q;
	while(q--)
	{
		cs++;
		cin >> n;
		for(ll i=1;i<=n;i++)
		{
			cin >> a[i] >> b[i];
		}
		cout << call(1,0) << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
#define ll long long
ll maxval = 1e18+7;
ll casio[300005][12],cs=0;
ll dp[300005][12],a[300005],b[300005];
ll n;
ll call(ll pos,ll inc)
{
    if(pos==n+1)return 0;
    if(casio[pos][inc]==cs)return dp[pos][inc];
    casio[pos][inc] = cs;
    ll ret = maxval;
    ll prev = a[pos-1]+inc;
    for(ll j = 0;j<=10;j++)
    {
        if(prev!=a[pos]+j)
        {
            ret = min(ret,call(pos+1,j)+(j*b[pos]));
        }
    }
    return dp[pos][inc] = ret;
}

int main()
{

    ll q;
    cin >> q;
    while(q--)
    {
        cs++;
        cin >> n;
        for(ll i=1;i<=n;i++)
        {
            cin >> a[i] >> b[i];
        }
        cout << call(1,0) << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " " << x << endl;
#define ll long long
ll maxval = 1e18+7;
ll casio[300005][12],cs=0;
ll dp[300005][12],a[300005],b[300005];
ll n;
ll call(ll pos,ll inc)
{
	if(pos==n+1)return 0;
	if(casio[pos][inc]==cs)return dp[pos][inc];
	casio[pos][inc] = cs;
	ll ret = maxval;
	ll prev = a[pos-1]+inc;
	for(ll j = 0;j<=10;j++)
	{
		if(prev!=a[pos]+j)
		{
			ret = min(ret,call(pos+1,j)+(j*b[pos]));
		}
	}
	return dp[pos][inc] = ret;
}

int main()
{

	ll q;
	cin >> q;
	while(q--)
	{
		cs++;
		cin >> n;
		for(ll i=1;i<=n;i++)
		{
			cin >> a[i] >> b[i];
		}
		cout << call(1,0) << endl;
	}
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long la;
// ����֤����󳬹�2��û�������
const int N=3e5+1;
la dp[N][3],a[N],c[N];
inline la Min(const la &x,const la &y){return x<y?x:y;}
int main(){
	int T,n;
	scanf("%d",&T);
	while(T--){
		memset(dp,0x7f,sizeof(dp));
		scanf("%d",&n);
		for(int i=1;i<=n;++i) scanf("%lld%lld",&a[i],&c[i]);
		dp[1][0]=0,dp[1][1]=c[1];
		for(int i=2;i<=n;++i){
			if(a[i]==a[i-1]){
				dp[i][0]=Min(dp[i-1][1],dp[i-1][2]);
				dp[i][1]=Min(dp[i-1][2],dp[i-1][0])+c[i];
				dp[i][2]=Min(dp[i-1][1],dp[i-1][0])+2*c[i];
			}
			else{
				for(int j=0;j<3;++j){
					for(int k=0;k<3;++k){
						if(a[i]+j!=a[i-1]+k) dp[i][j]=Min(dp[i][j],dp[i-1][k]+j*c[i]);
					}
				}
			}
		}
		printf("%lld\n",min(dp[n][0],dp[n][1]));
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long la;
// 可以证明变大超过2是没有意义的
const int N=3e5+1;
la dp[4][3],a[N],c[N];
inline la Min(const la &x,const la &y){return x<y?x:y;}
inline la in(){
	la x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') x=(x<<3ll)+(x<<1ll)+(la)(c^48),c=getchar();
	return x*f;
}
int main(){
	int T;la n;
	scanf("%d",&T);
	while(T--){
		memset(dp,0x7f,sizeof(dp));
		n=in();
		for(int i=1;i<=n;++i) a[i]=in(),c[i]=in();
		dp[1][0]=0,dp[1][1]=c[1],dp[1][2]=2*c[1];
		for(int i=2;i<=n;++i){
			int now=(i%4),pre=((i-1)%4);
			memset(dp[now],0x7f,sizeof(dp[now]));
			for(int j=0;j<=2;++j){
				for(int k=0;k<=2;++k){
					if(a[i]+j!=a[i-1]+k) dp[now][j]=Min(dp[now][j],dp[pre][k]+j*c[i]);
				}
			}
		}
		printf("%lld\n",Min(dp[n%4][0],Min(dp[n%4][1],dp[n%4][2])));
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long la;
// ����֤����󳬹�2��û�������
const int N=3e5+1;
la dp[4][3],a[N],c[N];
inline la Min(const la &x,const la &y){return x<y?x:y;}
inline la in(){
	la x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') x=(x<<3ll)+(x<<1ll)+(la)(c^48),c=getchar();
	return x*f;
}
int main(){
	int T;la n;
	scanf("%d",&T);
	while(T--){
		memset(dp,0x7f,sizeof(dp));
		n=in();
		for(int i=1;i<=n;++i) a[i]=in(),c[i]=in();
		dp[1][0]=0,dp[1][1]=c[1],dp[1][2]=2*c[1];
		for(int i=2;i<=n;++i){
			int now=(i%4),pre=((i-1)%4);
			memset(dp[now],0x7f,sizeof(dp[now]));
			for(int j=0;j<=2;++j){
				for(int k=0;k<=2;++k){
					if(a[i]+j!=a[i-1]+k) dp[now][j]=Min(dp[now][j],dp[pre][k]+j*c[i]);
				}
			}
		}
		printf("%lld\n",Min(dp[n%4][0],Min(dp[n%4][1],dp[n%4][2])));
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long la;
// ����֤����󳬹�2��û�������
const int N=3e5+1;
la dp[N][3],a[N],c[N];
inline la Min(const la &x,const la &y){return x<y?x:y;}
inline la in(){
	la x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') x=(x<<3ll)+(x<<1ll)+(la)(c^48),c=getchar();
	return x*f;
}
int main(){
	int T;la n;
	scanf("%d",&T);
	while(T--){
		memset(dp,0x7f,sizeof(dp));
		n=in();
		for(int i=1;i<=n;++i) a[i]=in(),c[i]=in();
		dp[1][0]=0,dp[1][1]=c[1];
		for(int i=2;i<=n;++i){
			if(a[i]==a[i-1]){
				dp[i][0]=Min(dp[i-1][1],dp[i-1][2]);
				dp[i][1]=Min(dp[i-1][2],dp[i-1][0])+c[i];
				dp[i][2]=Min(dp[i-1][1],dp[i-1][0])+2*c[i];
			}
			else{
				for(int j=0;j<3;++j){
					for(int k=0;k<3;++k){
						if(a[i]+j!=a[i-1]+k) dp[i][j]=Min(dp[i][j],dp[i-1][k]+j*c[i]);
					}
				}
			}
		}
		printf("%lld\n",Min(dp[n][0],dp[n][1]));
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long la;
// 可以证明变大超过2是没有意义的
const int N=3e5+1;
la dp[N][3],a[N],c[N];
int main(){
	int T,n;
	scanf("%d",&T);
	while(T--){
		memset(dp,0x7f,sizeof(dp));
		scanf("%d",&n);
		for(int i=1;i<=n;++i) scanf("%lld%lld",&a[i],&c[i]);
		dp[1][0]=0,dp[1][1]=c[1];
		for(int i=2;i<=n;++i){
			if(a[i]==a[i-1]){
				dp[i][0]=min(dp[i-1][1],dp[i-1][2]);
				dp[i][1]=min(dp[i-1][2],dp[i-1][0])+c[i];
				dp[i][2]=min(dp[i-1][1],dp[i-1][0])+2*c[i];
			}
			else{
				for(int j=0;j<3;++j){
					for(int k=0;k<3;++k){
						if(a[i]+j!=a[i-1]+k) dp[i][j]=min(dp[i][j],dp[i-1][k]+j*c[i]);
					}
				}
			}
		}
		printf("%lld\n",min(dp[n][0],dp[n][1]));
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long la;
// ����֤����󳬹�2��û�������
const int N=3e5+1;
la dp[4][3],a[N],c[N];
inline la Min(const la &x,const la &y){return x<y?x:y;}
inline la in(){
	la x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') x=(x<<3ll)+(x<<1ll)+(la)(c^48),c=getchar();
	return x*f;
}
int main(){
	int T;la n;
	scanf("%d",&T);
	while(T--){
		memset(dp,0x7f,sizeof(dp));
		n=in();
		for(int i=1;i<=n;++i) a[i]=in(),c[i]=in();
		dp[1][0]=0,dp[1][1]=c[1],dp[1][2]=2*c[1];
		for(int i=2;i<=n;++i){
			int now=(i%4),pre=((i-1)%4);
			for(int j=0;j<=2;++j){
				for(int k=0;k<=2;++k){
					if(a[i]+j!=a[i-1]+k) dp[now][j]=Min(dp[now][j],dp[pre][k]+j*c[i]);
				}
			}
		}
		printf("%lld\n",Min(dp[n%4][0],Min(dp[n%4][1],dp[n%4][2])));
	}
	return 0;
}


// youngjinp20
// 2019 09

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

typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

int main() {
	int q;
	cin >> q;
	for (int i=0; i<q; i++) {
		int n;
		int a[300000];
		int b[300000];
		int dp[300000][3] = {0};
		cin >> n;
		for (int j=1; j<=n; j++) {
			cin >> a[j] >> b[j];
			for (int k=0; k<3; k++) {
				int m=1e9;
				for (int l=0; l<3; l++) {
					if (l+a[j-1] != k+a[j]) {
						m = min(m, dp[j-1][l]);
					}
				}
				dp[j][k] = m+k*b[j];
			}
		}
		printf("%d\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
	}

	return 0;
}


// youngjinp20
// 2019 09

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

typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

int main() {
	int q;
	cin >> q;
	for (int i=0; i<q; i++) {
		int n;
		int a[300001];
		int b[300001];
		int dp[300001][3] = {0};
		cin >> n;
		for (int j=1; j<=n; j++) {
			cin >> a[j] >> b[j];
			for (int k=0; k<3; k++) {
				int m=1e9;
				for (int l=0; l<3; l++) {
					if (l+a[j-1] != k+a[j]) {
						m = min(m, dp[j-1][l]);
					}
				}
				dp[j][k] = m+k*b[j];
			}
		}
		printf("%d\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
	}

	return 0;
}


// youngjinp20
// 2019 09

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

typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

ll a[300005] = {0};
ll b[300005] = {0};
ll dp[300005][3] = {0};

int main() {
	int q;
	cin >> q;
	for (int i=0; i<q; i++) {
		int n;
		a[0] = -10;
		b[0] = 0;
		dp[0][0] = 0;
		dp[0][1] = 0;
		dp[0][2] = 0;
		cin >> n;
		for (int j=1; j<=n; j++) {
			cin >> a[j] >> b[j];
			for (int k=0; k<3; k++) {
				ll m=5e18;
				for (int l=0; l<3; l++) {
					if (l+a[j-1] != k+a[j]) {
						m = min(m, dp[j-1][l]+k*b[j]);
					}
				}
				dp[j][k] = m;
			}
		}
		printf("%lld\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
	}

	return 0;
}


// youngjinp20
// 2019 09

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

typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

ll a[300005] = {0};
ll b[300005] = {0};
ll dp[300005][3] = {0};

int main() {
	int q;
	cin >> q;
	for (int i=0; i<q; i++) {
		int n;
		a[0] = -1;
		b[0] = 0;
		dp[0][0] = 0;
		dp[0][1] = 0;
		dp[0][2] = 0;
		cin >> n;
		for (int j=1; j<=n; j++) {
			cin >> a[j] >> b[j];
			for (int k=0; k<3; k++) {
				ll m=1e9;
				for (int l=0; l<3; l++) {
					if (l+a[j-1] != k+a[j]) {
						m = min(m, dp[j-1][l]);
					}
				}
				dp[j][k] = m+k*b[j];
			}
		}
		printf("%lld\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
	}

	return 0;
}


// youngjinp20
// 2019 09

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

typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

int main() {
	int q;
	cin >> q;
	for (int i=0; i<q; i++) {
		int n;
		int a[300001];
		int b[300001];
		ll dp[300001][3] = {0};
		cin >> n;
		for (int j=1; j<=n; j++) {
			cin >> a[j] >> b[j];
			for (int k=0; k<3; k++) {
				ll m=1e9;
				for (int l=0; l<3; l++) {
					if (l+a[j-1] != k+a[j]) {
						m = min(m, dp[j-1][l]);
					}
				}
				dp[j][k] = m+k*b[j];
			}
		}
		printf("%lld\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
	}

	return 0;
}


// youngjinp20
// 2019 09

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

typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

ll a[300005] = {0};
ll b[300005] = {0};
ll dp[300005][3] = {0};

int main() {
	int q;
	scanf("%d", &q);
	for (int i=0; i<q; i++) {
		int n;
		a[0] = -10;
		b[0] = 0;
		dp[0][0] = 0;
		dp[0][1] = 0;
		dp[0][2] = 0;
		scanf("%d", &n);
		for (int j=1; j<=n; j++) {
			scanf("%d %d", &a[j], &b[j]);
			for (int k=0; k<3; k++) {
				ll m=5e18;
				for (int l=0; l<3; l++) {
					if (l+a[j-1] != k+a[j]) {
						m = min(m, dp[j-1][l]+k*b[j]);
					}
				}
				dp[j][k] = m;
			}
		}
		printf("%lld\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
	}

	return 0;
}


// youngjinp20
// 2019 09

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

typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
typedef vector<vi> vvi;

int main() {
	int q;
	cin >> q;
	for (int i=0; i<q; i++) {
		int n;
		ll a[300001];
		ll b[300001];
		ll dp[300001][3] = {0};
		cin >> n;
		for (int j=1; j<=n; j++) {
			cin >> a[j] >> b[j];
			for (int k=0; k<3; k++) {
				ll m=1e9;
				for (int l=0; l<3; l++) {
					if (l+a[j-1] != k+a[j]) {
						m = min(m, dp[j-1][l]);
					}
				}
				dp[j][k] = m+k*b[j];
			}
		}
		printf("%lld\n", min(dp[n][0], min(dp[n][1], dp[n][2])));
	}

	return 0;
}


#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

const int maxn = 8005;
int n;
ll a[maxn] , b[maxn];
int cnt;

int main(){
  cin >> n;
  for(int i = 1; i <= n; ++i){
    cin >> a[i];
  }
  for(int i = 1; i <= n; ++i){
    cin >> b[i];
  }
  unordered_map<ll , ll> mp;
  unordered_map<ll , ll> cnt;
  for(int i = 1; i <= n; ++i){
    mp[a[i]] += b[i];
    ++cnt[a[i]];
  }
  ll ret = 0;
  vector<ll> state;
  for(auto& it : cnt){
    if(it.second > 1){
      ret += mp[it.first];
      state.push_back(it.first);
      //mp.erase(it.first);
      mp[it.first] = 0;
    }
  }
  for(ll k : state){
    for(auto& i : mp){
      ll j = i.first;
      if((k & j) == j){
        ret += i.second;
        mp[j] = 0;
      }
    }
  }
  cout << ret << endl;
  return 0;
}


#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
int n;
ll a[maxn] , b[maxn];
ll dp[maxn][2];

int main(){
  int Q;
  cin >> Q;
  for(int q = 1; q <= Q; ++q){
    cin >> n;
    for(int i = 1; i <= n; ++i){
      cin >> a[i] >> b[i];
    }
    dp[0][0] = 0;
    dp[0][1] = 0;
    for(int i = 1; i <= n; ++i){
      if(a[i] == dp[i - 1][1]){
        if(b[i - 1] > b[i]){
          dp[i][0] = dp[i - 1][0] + b[i];
          dp[i][1] = a[i] + 1;
        }
        else{
          dp[i][0] = dp[i - 1][0] + b[i - 1];
          dp[i][1] = a[i];
        }
      }
      else{
        dp[i][0] = dp[i - 1][0];
        dp[i][1] = a[i];
      }
    }
    cout << dp[n][0] << endl;
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
const ll inf = 1ll << 50;
int n;
ll a[maxn] , b[maxn];
ll dp[maxn][3]; // dp[i][k] = minimal cost for increasing ai by k
// we will increase k by at most 2
// a , a + 1 , a + 2 , by pigeon hole principle , one can always use one element from
// this set and make sure it is different from 2 other adjcent elements

int main(){
  int Q;
  scanf("%d" , &Q);
  for(int q = 1; q <= Q; ++q){
    scanf("%d" , &n);
    for(int i = 1; i <= n; ++i){
      scanf("%d %d" , &a[i] , &b[i]);
      for(int j = 0; j < 3; ++j){
        dp[i][j] = inf;
      }
    }
    dp[1][0] = 0;
    dp[1][1] = b[1];
    dp[1][2] = b[1] * 2;
    ll ret = inf;
    for(int i = 2; i <= n; ++i){
      for(int j = 0; j < 3; ++j){
        for(int k = 0; k < 3; ++k){
          if(a[i - 1] + k != a[i] + j){
            if(dp[i][j] > dp[i - 1][k] + b[i] * j){
              dp[i][j] = dp[i - 1][k] + b[i] * j;
            }
          }
        }
      }
    }
    for(int i = 0; i < 3; ++i){
      if(ret > dp[n][i])ret = dp[n][i];
    }
    printf("%lld\n" , ret);
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
const ll inf = 1ll << 50;
int n;
ll a[maxn] , b[maxn];
ll dp[maxn][3]; // dp[i][k] = minimal cost for increasing ai by k
// we will increase k by at most 2
// a , a + 1 , a + 2 , by pigeon hole principle , one can always use one element from
// this set and make sure it is different from 2 other adjcent elements

int main(){
  int Q;
  scanf("%d" , &Q);
  for(int q = 1; q <= Q; ++q){
    scanf("%d" , &n);
    for(int i = 1; i <= n; ++i){
      scanf("%lld %lld" , &a[i] , &b[i]);
      for(int j = 0; j < 3; ++j){
        dp[i][j] = inf;
      }
    }
    dp[1][0] = 0;
    dp[1][1] = b[1];
    dp[1][2] = b[1] * 2;
    ll ret = inf;
    for(int i = 2; i <= n; ++i){
      for(int j = 0; j < 3; ++j){
        for(int k = 0; k < 3; ++k){
          if(a[i - 1] + k != a[i] + j){
            if(dp[i][j] > dp[i - 1][k] + b[i] * j){
              dp[i][j] = dp[i - 1][k] + b[i] * j;
            }
          }
        }
      }
    }
    for(int i = 0; i < 3; ++i){
      if(ret > dp[n][i])ret = dp[n][i];
    }
    printf("%lld\n" , ret);
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
const ll inf = 1ll << 50;
int n;
ll a[maxn] , b[maxn];
ll dp[maxn][3]; // dp[i][k] = minimal cost for increasing ai by k
// we will increase k by at most 2
// a , a + 1 , a + 2 , by pigeon hole principle , one can always use one element from
// this set and make sure it is different from 2 other adjcent elements

int main(){
  int Q;
  cin >> Q;
  for(int q = 1; q <= Q; ++q){
    cin >> n;
    for(int i = 1; i <= n; ++i){
      cin >> a[i] >> b[i];
      for(int j = 0; j < 3; ++j){
        dp[i][j] = inf;
      }
    }
    dp[1][0] = 0;
    dp[1][1] = b[1];
    dp[1][2] = b[1] * 2;
    ll ret = inf;
    for(int i = 2; i <= n; ++i){
      for(int j = 0; j < 3; ++j){
        for(int k = 0; k < 3; ++k){
          if(a[i - 1] + k != a[i] + j){
            if(dp[i][j] > dp[i - 1][k] + b[i] * j){
              dp[i][j] = dp[i - 1][k] + b[i] * j;
            }
          }
        }
      }
    }
    for(int i = 0; i < 3; ++i){
      if(ret > dp[n][i])ret = dp[n][i];
    }
    cout << ret << endl;
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
const ll inf = 1ll << 50;
int n;
ll a[maxn] , b[maxn];
ll dp[maxn][3]; // dp[i][k] = minimal cost for increasing ai by k
// we will increase k by at most 2
// a , a + 1 , a + 2 , by pigeon hole principle , one can always use one element from
// this set and make sure it is different from 2 other adjcent elements

int main(){
  int Q;
  cin >> Q;
  for(int q = 1; q <= Q; ++q){
    cin >> n;
    for(int i = 1; i <= n; ++i){
      cin >> a[i] >> b[i];
      for(int j = 0; j < 3; ++j){
        dp[i][j] = inf;
      }
    }
    dp[1][0] = 0;
    dp[1][1] = b[1];
    dp[1][2] = b[1] * 2;
    for(int i = 2; i <= n; ++i){
      for(int j = 0; j < 3; ++j){
        // find dp[i][j]
        for(int k = 0; k < 3; ++k){
          if(a[i - 1] + k != a[i] + j){
            dp[i][j] = min(dp[i][j] , dp[i - 1][k] + b[i] * j);
          }
        }
      }
    }
    cout << min(dp[n][0] , min(dp[n][1] , dp[n][2])) << endl;
  }
  return 0;
}


#include<bits/stdc++.h>
#define N 200005
using namespace std;
long long a[N],b[N];
int q,n;
long long dp[N][10];
int main()
{
//    freopen("inp.txt","r",stdin);
    cin>>q;
    while(q--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lld %lld",&a[i],&b[i]);
        for(int i=1;i<=n;i++)
            for(int j=0;j<=2;j++)
        {
            if(dp[i][j]) continue;
            dp[i][j]=0;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=2;j++)
            {
                long long tmp=1e18;
                for(int ij=0;ij<=2;ij++)
                {
                    if(a[i]+j==a[i-1]+ij) continue;
                    tmp=min(dp[i-1][ij],tmp);
                }
                dp[i][j]=tmp+b[i]*j;
            }
        }
        long long ans=1e18;
//        for(int i=1;i<=n;i++)
//        {
//            for(int j=0;j<=2;j++)
//                cout<<dp[i][j]<<" ";
//            cout<<"\n";
//        }
        for(int i=0;i<=2;i++)
            ans=min(ans,dp[n][i]);
        printf("%lld\n",ans);
    }
}


#include<bits/stdc++.h>
#define N 300005
using namespace std;
long long a[N],b[N];
int q,n;
long long dp[N][10];
int main()
{
//    freopen("inp.txt","r",stdin);
    cin>>q;
    while(q--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%lld %lld",&a[i],&b[i]);
        for(int i=1;i<=n;i++)
            for(int j=0;j<=2;j++)
        {
            if(dp[i][j]) continue;
            dp[i][j]=0;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=2;j++)
            {
                long long tmp=1e18;
                for(int ij=0;ij<=2;ij++)
                {
                    if(a[i]+j==a[i-1]+ij) continue;
                    tmp=min(dp[i-1][ij],tmp);
                }
                dp[i][j]=tmp+b[i]*j;
            }
        }
        long long ans=1e18;
//        for(int i=1;i<=n;i++)
//        {
//            for(int j=0;j<=2;j++)
//                cout<<dp[i][j]<<" ";
//            cout<<"\n";
//        }
        for(int i=0;i<=2;i++)
            ans=min(ans,dp[n][i]);
        printf("%lld\n",ans);
    }
}


#include<bits/stdc++.h>
#define N 300005
using namespace std;
int a[N],b[N];
int main()
{
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i]>>b[i];
        }
        long long sum=0;
        for(int i=2;i<=n;i++)
        {
            if(a[i]==a[i-1])
            {
                long long minn=1e18;
                if(b[i-1]<=b[i])
                {
                    a[i-1]++;
                    sum+=b[i-1];
                }
                else
                {
                    a[i]++;
                    sum+=b[i];
                }
            }
        }
        cout<<sum<<"\n";
    }
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <fstream>
#include <math.h>
#include <queue>
#include <bitset>
#include <cmath>
#include <complex>
#include <iomanip>
#include <valarray>
#include <stack>
#include <cstring>

using namespace std;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef vector<ull> vull;
typedef set<string> ss;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef vector<viii> vviii;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef map<string, int> msi;
typedef multiset<int> mlsi;
typedef set<ii> sii;
typedef map<int, ii> mii;
typedef pair<string, int> psi;
typedef queue<int> qi;
typedef pair<int, vi> pivi;
typedef vector<pivi> vpivi;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef pair<int, bool> ib;
typedef vector<ib> vib;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<vib> vvib;
typedef set<int> si;
typedef pair<bool, bool> bb;
typedef long long ll;
typedef pair<ll, ll>pll;
typedef vector<ll>vll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<vll> vvll;

#define PI 3.14159265

/* ii;
class SegTree {
public:
   vii A;
   vi st;
   int n;

   int left(int i) {
       return (i << 1);
   }

   int right(int i) {
       return (i << 1) + 1;
   }

   void build(int p, int L, int R) {
       if (L == R)
           st[p] = L;
       else {
           build(left(p), L, (L + R) / 2);
           build(right(p), (L + R) / 2 + 1, R);
           int p1 = st[left(p)];
           int p2 = st[right(p)];
           st[p] = A[p1].second <= A[p2].second ? p1 : p2;
       }
   }

   SegTree(const vii &_A) {
       A = _A;
       n = _A.size();
       st.assign(4 * n, -1);
       build(1, 0, n - 1);
   }

   int rmq(int p, int L, int R, int i, int j) {
       if (i > R || j < L) return -1;
       if (L >= i && R <= j) return st[p];
       int p1 = rmq(left(p), L, (L + R) / 2, i, j);
       int p2 = rmq(right(p), (L + R) / 2 + 1, R, i, j);
       if (p1 == -1) return p2;
       if (p2 == -1) return p1;

       return (A[p1].second <= A[p2].second ? p1 : p2);
   }

   int rmq(int i, int j) {
       return rmq(1, 0, n - 1, i, j);
   }

   int getIndex(int p, int L, int R, int pos) {
       if (L == R) return p;
       if (pos <= (L + R) / 2) return getIndex(left(p), L, (L + R) / 2, pos);
       return getIndex(right(p), (L + R) / 2 + 1, R, pos);
   }

   void update(int pos, int value) {
       A[pos].second = value;
       pos = getIndex(1, 0, n - 1, pos);
       pos /= 2;

       while (pos != 0) {
           int p1 = st[left(pos)];
           int p2 = st[right(pos)];

           if (p1 >= 0 && p2 >= 0)
               st[pos] = A[p1].second <= A[p2].second ? p1 : p2;
           pos /= 2;
       }
   }
};
 */

class SegTree {
public:
    vll A;
    vll st;
    int n;

    int left(int i) {
        return (i << 1);
    }

    int right(int i) {
        return (i << 1) + 1;
    }

    void build(int p, int L, int R) {
        if (L == R)
            st[p] = L;
        else {
            build(left(p), L, (L + R) / 2);
            build(right(p), (L + R) / 2 + 1, R);
            int p1 = st[left(p)];
            int p2 = st[right(p)];
            st[p] = A[p1] >= A[p2] ? p1 : p2;
        }
    }

    SegTree(const vll &_A) {
        A = _A;
        n = _A.size();
        st.assign(4 * n, -1);
        build(1, 0, n - 1);
    }

    int rmq(int p, int L, int R, int i, int j) {
        if (i > R || j < L) return -1;
        if (L >= i && R <= j) return st[p];
        int p1 = rmq(left(p), L, (L + R) / 2, i, j);
        int p2 = rmq(right(p), (L + R) / 2 + 1, R, i, j);
        if (p1 == -1) return p2;
        if (p2 == -1) return p1;

        return (A[p1] >= A[p2] ? p1 : p2);
    }

    int rmq(int i, int j) {
        if (i < 0)
            i = 0;

        return rmq(1, 0, n - 1, i, j);
    }

    int getIndex(int p, int L, int R, int pos) {
        if (L == R) return p;
        if (pos <= (L + R) / 2) return getIndex(left(p), L, (L + R) / 2, pos);
        return getIndex(right(p), (L + R) / 2 + 1, R, pos);
    }

    void update(int pos, int value) {
        A[pos] = value;
        pos = getIndex(1, 0, n - 1, pos);
        pos /= 2;

        while (pos != 0) {
            int p1 = st[left(pos)];
            int p2 = st[right(pos)];

            if (p1 >= 0 && p2 >= 0)
                st[pos] = A[p1] >= A[p2] ? p1 : p2;
            pos /= 2;
        }
    }
};

class UnionFind {
public:
    vi rank;
    vii p;

    UnionFind(int N) {
        rank.assign(N, 0);
        p.assign(N, ii(0, 1));
        for (int i = 0; i < N; i++) p[i].first = i;
    }

    int findSet(int i) {
        return (p[i].first == i) ? i : (p[i].first = findSet(p[i].first));
    }

    bool isSameSet(int i, int j) {
        return findSet(i) == findSet(j);
    }

    void unionSet(int i, int j) {
        if (!isSameSet(i, j)) {
            int x = findSet(i), y = findSet(j);
            if (rank[x] > rank[y]) {
                p[y].first = x;
                p[x].second += p[y].second;
            } else {
                p[x].first = y;
                p[y].second += p[x].second;
                if (rank[x] == rank[y]) rank[y]++;
            }
        }
    }

    int getMaxId() {
        int maxIn = 0;
        for (int i = 0; i < p.size(); i++)
            if (p[i].second > p[maxIn].second)
                maxIn = i;

        return maxIn;
    }

    int getNum() {
        si par;

        for (int i = 0; i < p.size(); i++)
            par.insert(p[i].first);
        return par.size();
    }

};

int main() {

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

    int tc;
    cin >> tc;
    while (tc--) {
        int n;
        cin >> n;
        vull a, b;

        a.assign(n, 0);
        b.assign(n, 0);

        for (int i = 0; i < n; i++) cin >> a[i] >> b[i];

        if (n == 1) {
            cout << "0\n";
            continue;
        }

        ull dp[n][3];
        dp[0][0] = 0;
        dp[0][1] = b[0];
        dp[0][2] = 2 * b[0];

        for (int i = 1; i < n; i++) {
            dp[i][0] = (1ull << 60);
            dp[i][1] = (1ull << 60);
            dp[i][2] = (1ull << 60);

            for (int j = 0; j < 3; j++)
                for (int k = 0; k < 3; k++)
                    if(a[i]+j != a[i-1]+k)
                        dp[i][j] = min(dp[i][j] , dp[i-1][k] + (j*b[i]));
        }

        ull res = min(dp[n-1][0] , dp[n-1][1]);
        res = min(res , dp[n-1][2]);

        cout << res << endl;
    }

}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <fstream>
#include <math.h>
#include <queue>
#include <bitset>
#include <cmath>
#include <complex>
#include <iomanip>
#include <valarray>
#include <stack>
#include <cstring>

using namespace std;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef vector<ull> vull;
typedef set<string> ss;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef vector<viii> vviii;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef map<string, int> msi;
typedef multiset<int> mlsi;
typedef set<ii> sii;
typedef map<int, ii> mii;
typedef pair<string, int> psi;
typedef queue<int> qi;
typedef pair<int, vi> pivi;
typedef vector<pivi> vpivi;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef pair<int, bool> ib;
typedef vector<ib> vib;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<vib> vvib;
typedef set<int> si;
typedef pair<bool, bool> bb;
typedef long long ll;
typedef pair<ll, ll>pll;
typedef vector<ll>vll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<vll> vvll;

#define PI 3.14159265

/* ii;
class SegTree {
public:
   vii A;
   vi st;
   int n;

   int left(int i) {
       return (i << 1);
   }

   int right(int i) {
       return (i << 1) + 1;
   }

   void build(int p, int L, int R) {
       if (L == R)
           st[p] = L;
       else {
           build(left(p), L, (L + R) / 2);
           build(right(p), (L + R) / 2 + 1, R);
           int p1 = st[left(p)];
           int p2 = st[right(p)];
           st[p] = A[p1].second <= A[p2].second ? p1 : p2;
       }
   }

   SegTree(const vii &_A) {
       A = _A;
       n = _A.size();
       st.assign(4 * n, -1);
       build(1, 0, n - 1);
   }

   int rmq(int p, int L, int R, int i, int j) {
       if (i > R || j < L) return -1;
       if (L >= i && R <= j) return st[p];
       int p1 = rmq(left(p), L, (L + R) / 2, i, j);
       int p2 = rmq(right(p), (L + R) / 2 + 1, R, i, j);
       if (p1 == -1) return p2;
       if (p2 == -1) return p1;

       return (A[p1].second <= A[p2].second ? p1 : p2);
   }

   int rmq(int i, int j) {
       return rmq(1, 0, n - 1, i, j);
   }

   int getIndex(int p, int L, int R, int pos) {
       if (L == R) return p;
       if (pos <= (L + R) / 2) return getIndex(left(p), L, (L + R) / 2, pos);
       return getIndex(right(p), (L + R) / 2 + 1, R, pos);
   }

   void update(int pos, int value) {
       A[pos].second = value;
       pos = getIndex(1, 0, n - 1, pos);
       pos /= 2;

       while (pos != 0) {
           int p1 = st[left(pos)];
           int p2 = st[right(pos)];

           if (p1 >= 0 && p2 >= 0)
               st[pos] = A[p1].second <= A[p2].second ? p1 : p2;
           pos /= 2;
       }
   }
};
 */

class SegTree {
public:
    vll A;
    vll st;
    int n;

    int left(int i) {
        return (i << 1);
    }

    int right(int i) {
        return (i << 1) + 1;
    }

    void build(int p, int L, int R) {
        if (L == R)
            st[p] = L;
        else {
            build(left(p), L, (L + R) / 2);
            build(right(p), (L + R) / 2 + 1, R);
            int p1 = st[left(p)];
            int p2 = st[right(p)];
            st[p] = A[p1] >= A[p2] ? p1 : p2;
        }
    }

    SegTree(const vll &_A) {
        A = _A;
        n = _A.size();
        st.assign(4 * n, -1);
        build(1, 0, n - 1);
    }

    int rmq(int p, int L, int R, int i, int j) {
        if (i > R || j < L) return -1;
        if (L >= i && R <= j) return st[p];
        int p1 = rmq(left(p), L, (L + R) / 2, i, j);
        int p2 = rmq(right(p), (L + R) / 2 + 1, R, i, j);
        if (p1 == -1) return p2;
        if (p2 == -1) return p1;

        return (A[p1] >= A[p2] ? p1 : p2);
    }

    int rmq(int i, int j) {
        if (i < 0)
            i = 0;

        return rmq(1, 0, n - 1, i, j);
    }

    int getIndex(int p, int L, int R, int pos) {
        if (L == R) return p;
        if (pos <= (L + R) / 2) return getIndex(left(p), L, (L + R) / 2, pos);
        return getIndex(right(p), (L + R) / 2 + 1, R, pos);
    }

    void update(int pos, int value) {
        A[pos] = value;
        pos = getIndex(1, 0, n - 1, pos);
        pos /= 2;

        while (pos != 0) {
            int p1 = st[left(pos)];
            int p2 = st[right(pos)];

            if (p1 >= 0 && p2 >= 0)
                st[pos] = A[p1] >= A[p2] ? p1 : p2;
            pos /= 2;
        }
    }
};

class UnionFind {
public:
    vi rank;
    vii p;

    UnionFind(int N) {
        rank.assign(N, 0);
        p.assign(N, ii(0, 1));
        for (int i = 0; i < N; i++) p[i].first = i;
    }

    int findSet(int i) {
        return (p[i].first == i) ? i : (p[i].first = findSet(p[i].first));
    }

    bool isSameSet(int i, int j) {
        return findSet(i) == findSet(j);
    }

    void unionSet(int i, int j) {
        if (!isSameSet(i, j)) {
            int x = findSet(i), y = findSet(j);
            if (rank[x] > rank[y]) {
                p[y].first = x;
                p[x].second += p[y].second;
            } else {
                p[x].first = y;
                p[y].second += p[x].second;
                if (rank[x] == rank[y]) rank[y]++;
            }
        }
    }

    int getMaxId() {
        int maxIn = 0;
        for (int i = 0; i < p.size(); i++)
            if (p[i].second > p[maxIn].second)
                maxIn = i;

        return maxIn;
    }

    int getNum() {
        si par;

        for (int i = 0; i < p.size(); i++)
            par.insert(p[i].first);
        return par.size();
    }

};

int main() {

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

    int tc;
    cin >> tc;
    while (tc--) {
        int n;
        cin >> n;
        vull a, b;

        a.assign(n, 0);
        b.assign(n, 0);

        for (int i = 0; i < n; i++) cin >> a[i] >> b[i];

        if (n == 1) {
            cout << "0\n";
            continue;
        }

        ull dp[n][3];
        dp[0][0] = 0;
        dp[0][1] = b[0];
        dp[0][2] = 2 * b[0];

        for (int i = 1; i < n; i++) {
            dp[i][0] = (1ull << 60);
            dp[i][1] = (1ull << 60);
            dp[i][2] = (1ull << 60);

            for (int j = 0; j < 3; j++)
                for (int k = 0; k < 3; k++)
                    if (a[i] + j != a[i - 1] + k)
                        dp[i][j] = min(dp[i][j], dp[i - 1][k] + (j * b[i]));
        }

        ull res = min(dp[n - 1][0], dp[n - 1][1]);
        res = min(res, dp[n - 1][2]);

        cout << res << endl;
    }

}