You have a fence consisting of $n$ vertical boards. The width of each board is $1$. The height of the $i$-th board is $a_i$. You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from $2$ to $n$, the condition $a_{i-1} \neq a_i$ holds.

Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the $i$-th board by $1$, but you have to pay $b_i$ rubles for it. The length of each board can be increased any number of times (possibly, zero).

Calculate the minimum number of rubles you have to spend to make the fence great again!

You have to answer $q$ independent queries.


-----Input-----

The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.

The first line of each query contains one integers $n$ ($1 \le n \le 3 \cdot 10^5$) — the number of boards in the fence.

The following $n$ lines of each query contain the descriptions of the boards. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$) — the length of the $i$-th board and the price for increasing it by $1$, respectively.

It is guaranteed that sum of all $n$ over all queries not exceed $3 \cdot 10^5$.

It is guaranteed that answer to each query will not exceed $10^{18}$.


-----Output-----

For each query print one integer — the minimum number of rubles you have to spend to make the fence great.


-----Example-----
Input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2

Output
2
9
0



-----Note-----

In the first query you have to increase the length of second board by $2$. So your total costs if $2 \cdot b_2 = 2$.

In the second query you have to increase the length of first board by $1$ and the length of third board by $1$. So your total costs if $1 \cdot b_1 + 1 \cdot b_3 = 9$.

In the third query the fence is great initially, so you don't need to spend rubles.

Difficulty: interview

Starter code:

Solutions:
3

import math
import os
import sys


DEBUG = 'DEBUG' in os.environ


def inp():
    return sys.stdin.readline().rstrip()


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


INF = 10 ** 20


def solve(N, A, B):
    dp = {A[0]: 0, A[0] + 1: B[0], A[0] + 2: B[0] * 2}
    for i in range(1, N):
        ndp = {}

        h = A[i]
        for ph, c in dp.items():
            for inc in range(3):
                nh = h + inc
                if ph == nh:
                    continue
                if nh not in ndp:
                    ndp[nh] = INF
                ndp[nh] = min(ndp[nh], c + B[i] * inc)

        dp = ndp

    return min(dp.values())


def main():
    Q = int(inp())
    for _ in range(Q):
        N = int(inp())
        A = []
        B = []
        for _ in range(N):
            a, b = [int(e) for e in inp().split()]
            A.append(a)
            B.append(b)
        print(solve(N, A, B))


def __starting_point():
    main()

__starting_point()

import sys
input = sys.stdin.readline

q=int(input())

for testcases in range(q):
    n=int(input())
    f=[tuple(map(int,input().split())) for i in range(n)]

    
    DP0=[0]*n
    DP1=[0]*n
    DP2=[0]*n

    DP1[0]=f[0][1]
    DP2[0]=f[0][1]*2

    for i in range(1,n):
        x=f[i-1][0]
        y=f[i][0]

        if y==x:
            DP0[i]=min(DP1[i-1],DP2[i-1])
            DP1[i]=f[i][1]+min(DP0[i-1],DP2[i-1])
            DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1])

        elif y==x+1:
            DP0[i]=min(DP0[i-1],DP2[i-1])
            DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1])
            DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1],DP2[i-1])

        elif y==x+2:
            DP0[i]=min(DP0[i-1],DP1[i-1])
            DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1],DP2[i-1])
            DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1],DP2[i-1])

        elif y==x-1:
            DP0[i]=min(DP0[i-1],DP1[i-1],DP2[i-1])
            DP1[i]=f[i][1]+min(DP1[i-1],DP2[i-1])
            DP2[i]=f[i][1]*2+min(DP0[i-1],DP2[i-1])

        elif y==x-2:
            DP0[i]=min(DP0[i-1],DP1[i-1],DP2[i-1])
            DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1],DP2[i-1])
            DP2[i]=f[i][1]*2+min(DP1[i-1],DP2[i-1])
            
        else:
            DP0[i]=min(DP0[i-1],DP1[i-1],DP2[i-1])
            DP1[i]=f[i][1]+min(DP0[i-1],DP1[i-1],DP2[i-1])
            DP2[i]=f[i][1]*2+min(DP0[i-1],DP1[i-1],DP2[i-1])

    print(min(DP0[n-1],DP1[n-1],DP2[n-1]))
            
            
            

    


import sys
input = sys.stdin.readline

Q = int(input())
for _ in range(Q):
    N = int(input())
    X = []
    ne = [0] * 3
    for i in range(N):
        a, b = list(map(int, input().split()))
        X.append((a, b))
        if i == 0:
            ne = [0, b, b*2]
            continue
        Y = ne
        ne = [1<<100] * 3
        if X[i][0] != X[i-1][0]: ne[0] = min(ne[0], Y[0])
        if X[i][0] != X[i-1][0]+1: ne[0] = min(ne[0], Y[1])
        if X[i][0] != X[i-1][0]+2: ne[0] = min(ne[0], Y[2])
        if X[i][0]+1 != X[i-1][0]: ne[1] = min(ne[1], Y[0] + b)
        if X[i][0]+1 != X[i-1][0]+1: ne[1] = min(ne[1], Y[1] + b)
        if X[i][0]+1 != X[i-1][0]+2: ne[1] = min(ne[1], Y[2] + b)
        if X[i][0]+2 != X[i-1][0]: ne[2] = min(ne[2], Y[0] + b * 2)
        if X[i][0]+2 != X[i-1][0]+1: ne[2] = min(ne[2], Y[1] + b * 2)
        if X[i][0]+2 != X[i-1][0]+2: ne[2] = min(ne[2], Y[2] + b * 2)
        
    print(min(ne))


from sys import stdin
input = stdin.readline
q = int(input())
for rwe in range(q):
	n = int(input())
	a = [0] * n
	b = [0] * n
	for i in range(n):
		x, y = map(int, input().split())
		a[i] = x
		b[i] = y
	dp = [[0,0,0] for i in range(n)]
	dp[0][0] = 0
	dp[0][1] = b[0]
	dp[0][2] = 2 * b[0]
	for i in range(1, n):
		for pod in range(3):
			x = (dp[i-1][0] if a[i-1] != a[i] + pod else 100000000000000000000000000)
			y = (dp[i-1][1] if a[i-1] + 1 != a[i] + pod else 10000000000000000000000000000)
			z = (dp[i-1][2] if a[i-1] + 2 != a[i] + pod else 10000000000000000000000000000)
			dp[i][pod] = min(x, y, z) + pod*b[i]
	print(min(dp[n-1]))

''' CODED WITH LOVE BY SATYAM KUMAR '''

from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys

sys.setrecursionlimit(10 ** 6)  # max depth of recursion
threading.stack_size(2 ** 27)  # new thread will get stack of such size
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7


class MergeFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n
        self.num_sets = n
        self.lista = [[_] for _ in range(n)]

    def find(self, a):
        to_update = []
        while a != self.parent[a]:
            to_update.append(a)
            a = self.parent[a]
        for b in to_update:
            self.parent[b] = a
        return self.parent[a]

    def merge(self, a, b):
        a = self.find(a)
        b = self.find(b)
        if a == b:
            return
        if self.size[a] < self.size[b]:
            a, b = b, a
        self.num_sets -= 1
        self.parent[b] = a
        self.size[a] += self.size[b]
        self.lista[a] += self.lista[b]

    def set_size(self, a):
        return self.size[self.find(a)]

    def __len__(self):
        return self.num_sets


def display(string_to_print):
    stdout.write(str(string_to_print) + "\n")


def prime_factors(n):  # n**0.5 complex
    factors = dict()
    for i in range(2, math.ceil(math.sqrt(n)) + 1):
        while n % i == 0:
            if i in factors:
                factors[i] += 1
            else:
                factors[i] = 1
            n = n // i
    if n > 2:
        factors[n] = 1
    return (factors)


def all_factors(n):
    return set(reduce(list.__add__,
                      ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))


def fibonacci_modP(n, MOD):
    if n < 2: return 1
    return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(
        fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD


def factorial_modP_Wilson(n, p):
    if (p <= n):
        return 0
    res = (p - 1)
    for i in range(n + 1, p):
        res = (res * cached_fn(InverseEuler, i, p)) % p
    return res


def binary(n, digits=20):
    b = bin(n)[2:]
    b = '0' * (digits - len(b)) + b
    return b


def is_prime(n):
    """Returns True if n is prime."""
    if n < 4:
        return True
    if n % 2 == 0:
        return False
    if n % 3 == 0:
        return False
    i = 5
    w = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += w
        w = 6 - w
    return True


def generate_primes(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    return prime


factorial_modP = []


def warm_up_fac(MOD):
    nonlocal factorial_modP, fac_warm_up
    if fac_warm_up: return
    factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
    for i in range(2, fac_warm_up_size):
        factorial_modP[i] = (factorial_modP[i - 1] * i) % MOD
    fac_warm_up = True


def InverseEuler(n, MOD):
    return pow(n, MOD - 2, MOD)


def nCr(n, r, MOD):
    nonlocal fac_warm_up, factorial_modP
    if not fac_warm_up:
        warm_up_fac(MOD)
        fac_warm_up = True
    return (factorial_modP[n] * (
            (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD)) % MOD)) % MOD


def test_print(*args):
    if testingMode:
        print(args)


def display_list(list1, sep=" "):
    stdout.write(sep.join(map(str, list1)) + "\n")


def display_2D_list(li):
    for i in li:
        print(i)


def prefix_sum(li):
    sm = 0
    res = []
    for i in li:
        sm += i
        res.append(sm)
    return res


def get_int():
    return int(stdin.readline().strip())


def get_tuple():
    return list(map(int, stdin.readline().split()))


def get_list():
    return list(map(int, stdin.readline().split()))


memory = dict()


def clear_cache():
    nonlocal memory
    memory = dict()


def cached_fn(fn, *args):
    nonlocal memory
    if args in memory:
        return memory[args]
    else:
        result = fn(*args)
        memory[args] = result
        return result


def ncr(n, r):
    return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))


def binary_search(i, li):
    fn = lambda x: li[x] - x // i
    x = -1
    b = len(li)
    while b >= 1:
        while b + x < len(li) and fn(b + x) > 0:  # Change this condition 2 to whatever you like
            x += b
        b = b // 2
    return x


# -------------------------------------------------------------- MAIN PROGRAM


TestCases = True
fac_warm_up_size = 10 ** 5 + 100
optimise_for_recursion = False  # Can not be used clubbed with TestCases WHen using recursive functions, use Python 3


def main():
    n = get_int()
    lengths = []
    costs = []

    for _ in range(n):
        a, b = get_tuple()
        lengths.append(a)
        costs.append(b)
    dp = [[0, costs[0], 2*costs[0]]]
    for i in range(1, n):
        kt = [10 ** 20] * 3
        for k in range(3):
            for j, ele in enumerate(dp[-1]):
                if lengths[i-1]+j!=lengths[i]+k:
                    kt[k] = min(kt[k], ele+costs[i]*k)
        dp.append(kt)
    #print(dp)
    print(min(dp[-1]))


# --------------------------------------------------------------------- END=


if TestCases:
    for i in range(get_int()):
        main()
else:
    main() if not optimise_for_recursion else threading.Thread(target=main).start()


import sys
input = sys.stdin.readline

q = int(input())
for i in range(q):
    n = int(input())
    dp = [[0] * n for _ in range(3)]
    prev = 0
    for i in range(n):
        l, c = [int(item) for item in input().split()]
        if i == 0:
            dp[0][0] = 0
            dp[1][0] = c
            dp[2][0] = c * 2
            prev = l
            continue
        prev_min = min(dp[0][i-1], dp[1][i-1], dp[2][i-1])
        if l > prev + 2:
            dp[0][i] = prev_min
            dp[1][i] = prev_min + c
            dp[2][i] = prev_min + c * 2
        elif l == prev + 2:
            dp[0][i] = min(dp[0][i-1], dp[1][i-1])
            dp[1][i] = prev_min + c
            dp[2][i] = prev_min + c * 2
        elif l == prev + 1:
            dp[0][i] = min(dp[0][i-1], dp[2][i-1]) 
            dp[1][i] = min(dp[0][i-1], dp[1][i-1]) + c
            dp[2][i] = prev_min + c * 2
        elif l == prev:
            dp[0][i] = min(dp[1][i-1], dp[2][i-1])
            dp[1][i] = min(dp[0][i-1], dp[2][i-1]) + c
            dp[2][i] = min(dp[0][i-1], dp[1][i-1]) + 2 * c
        elif l == prev - 1:
            dp[0][i] = prev_min
            dp[1][i] = min(dp[1][i-1], dp[2][i-1]) + c
            dp[2][i] = min(dp[0][i-1], dp[2][i-1]) + 2 * c
        elif l == prev - 2:
            dp[0][i] = prev_min
            dp[1][i] = prev_min + c
            dp[2][i] = min(dp[1][i-1], dp[2][i-1]) + 2 * c
        elif l < prev - 2:
            dp[0][i] = prev_min
            dp[1][i] = prev_min + c
            dp[2][i] = prev_min + c * 2
        prev = l
    print(min(dp[0][n-1], dp[1][n-1], dp[2][n-1]))

import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
    n = int(input())
    d = [[-100, 0]] + [list(map(int, input().split())) for _ in range(n)]
    dp = [[100 for j in range(3)] for i in range(n+1)]
    dp[0] = [0, 0, 0, 0]
    for i in range(n):
        if d[i+1][0] == d[i][0]:
            dp[i+1][0] = min(dp[i][1], dp[i][2])
            dp[i+1][1] = min(dp[i][0], dp[i][2]) + d[i+1][1]
            dp[i+1][2] = min(dp[i][0], dp[i][1]) + d[i+1][1] * 2
        elif d[i+1][0] + 1 == d[i][0]:
            dp[i+1][0] = min(dp[i])
            dp[i+1][1] = min(dp[i][1], dp[i][2]) + d[i+1][1]
            dp[i+1][2] = min(dp[i][0], dp[i][1]) + d[i+1][1] * 2
        elif d[i+1][0] + 2 == d[i][0]:
            dp[i+1][0] = min(dp[i])
            dp[i+1][1] = min(dp[i]) + d[i+1][1]
            dp[i+1][2] = min(dp[i][1], dp[i][2]) + d[i+1][1] * 2
        elif d[i+1][0] - 1 == d[i][0]:
            dp[i+1][0] = min(dp[i][0], dp[i][2])
            dp[i+1][1] = min(dp[i][1], dp[i][0]) + d[i+1][1]
            dp[i+1][2] = min(dp[i]) + d[i+1][1] * 2
        elif d[i+1][0] - 2 == d[i][0]:
            dp[i+1][0] = min(dp[i][0], dp[i][1])
            dp[i+1][1] = min(dp[i]) + d[i+1][1]
            dp[i+1][2] = min(dp[i]) + d[i+1][1] * 2
        else:
            dp[i+1][0] = min(dp[i])
            dp[i+1][1] = min(dp[i]) + d[i+1][1]
            dp[i+1][2] = min(dp[i]) + d[i+1][1] * 2
    print(min(dp[n]))


import sys
input=sys.stdin.readline
q=int(input())
for _ in range(q):
  n=int(input())
  ans=0
  a,b=list(map(int,input().split()))
  cur=a
  A=[0,b,2*b]
  for i in range(n-1):
    a=cur
    na,nb=list(map(int,input().split()))
    cur=na
    a0,a1,a2=A
    if na==a:
      A[0]=min(a1,a2)
      A[1]=nb+min(a0,a2)
      A[2]=2*nb+min(a0,a1)
    elif na==a-1:
      A[0]=min(a0,a1,a2)
      A[1]=nb+min(a1,a2)
      A[2]=2*nb+min(a0,a2)
    elif na==a-2:
      A[0]=min(a0,a1,a2)
      A[1]=nb+min(a0,a1,a2)
      A[2]=2*nb+min(a1,a2)
    elif na==a+1:
      A[0]=min(a0,a2)
      A[1]=nb+min(a0,a1)
      A[2]=2*nb+min(a0,a1,a2)
    elif na==a+2:
      A[0]=min(a0,a1)
      A[1]=nb+min(a0,a1,a2)
      A[2]=2*nb+min(a0,a1,a2)
    else:
      A[0]=min(a0,a1,a2)
      A[1]=nb+min(a0,a1,a2)
      A[2]=2*nb+min(a0,a1,a2)
 
  print(min(A))


import sys
input = sys.stdin.readline

Q = int(input())
data = []
for _ in range(Q):
    N = int(input())
    A = []
    B = []
    for _ in range(N):
        a, b = list(map(int, input().split()))
        A.append(a)
        B.append(b)
    data.append([N, A, B])

for N, A, B in data:
    dp = [[0, 0, 0] for _ in range(N)]
    dp[0][1] = B[0]
    dp[0][2] = 2*B[0]
    for i in range(1, N):
        if A[i] == A[i-1]:
            dp[i][0] = min(dp[i-1][1], dp[i-1][2])
        elif A[i] == A[i-1]+1:
            dp[i][0] = min(dp[i-1][0], dp[i-1][2])
        elif A[i] == A[i-1]+2:
            dp[i][0] = min(dp[i-1][0], dp[i-1][1])
        else:
            dp[i][0] = min([dp[i-1][0], dp[i-1][1], dp[i-1][2]])
        
        if A[i] == A[i-1]+1:
            dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + B[i]
        elif A[i] == A[i-1]:
            dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + B[i]
        elif A[i] == A[i-1]-1:
            dp[i][1] = min(dp[i-1][1], dp[i-1][2]) + B[i]
        else:
            dp[i][1] = min([dp[i-1][0], dp[i-1][1], dp[i-1][2]]) + B[i]
        
        if A[i] == A[i-1]:
            dp[i][2] = min(dp[i-1][0], dp[i-1][1]) + 2*B[i]
        elif A[i] == A[i-1]-1:
            dp[i][2] = min(dp[i-1][0], dp[i-1][2]) + 2*B[i]
        elif A[i] == A[i-1]-2:
            dp[i][2] = min(dp[i-1][1], dp[i-1][2]) + 2*B[i]
        else:
            dp[i][2] = min([dp[i-1][0], dp[i-1][1], dp[i-1][2]]) + 2*B[i]
    ans = min(dp[N-1])
    print(ans)



'''input
3
3
2 4
2 1
3 5
3
2 3
2 10
2 6
4
1 7
3 3
2 6
1000000000 2
'''
from sys import stdin
from math import ceil, log


# main starts
q = int(stdin.readline().strip())
for _ in range(q):
	n = int(stdin.readline().strip())
	h = []
	c  = []
	dp = dict()
	for i in range(n):
		a, b = list(map(int, stdin.readline().split()))
		h.append(a)
		c.append(b)
	dp = [[0 for x in range(3)] for y in range(n)]
	dp[0][0] = 0
	dp[0][1] = c[0]
	dp[0][2] = 2*c[0]
	for i in range(1, n):
		if h[i] == h[i - 1]:
			dp[i][0] = min(dp[i - 1][1], dp[i- 1][2])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + c[i]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * c[i]
		elif h[i] + 1 == h[i - 1]:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + c[i]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + 2 * c[i]
		elif h[i] + 2 == h[i - 1]:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1])+ c[i]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * c[i]
		elif h[i] == h[i - 1] + 1:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][2])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + c[i]
			dp[i][2] = min(dp[i - 1]) + 2 * c[i]
		elif h[i] == h[i - 1] + 2:
			dp[i][0] = min(dp[i - 1][:2])
			dp[i][1] = min(dp[i - 1])+ c[i]
			dp[i][2] = min(dp[i - 1]) + 2 * c[i]

		else:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1])+ c[i]
			dp[i][2] = min(dp[i - 1]) + 2 * c[i]
	print(min(dp[-1]))

import sys
from math import inf as inf
for _ in range(int(input())):
    n=int(sys.stdin.readline())
    dp=[[inf,inf,inf] for i in range(n+1)]
    a=[]
    for i in range(n):
        a.append(list(map(int,sys.stdin.readline().split())))
    dp[0][0]=0
    dp[0][1]=a[0][1]
    dp[0][2]=2*a[0][1]
    for i in range(1,n):
        for j in range(3):
            for k in range(3):
                if a[i][0] + j != a[i-1][0] + k:
                    dp[i][j]=min(dp[i][j],dp[i-1][k] + j*a[i][1])
    # print(dp)                
    print(min(dp[n-1]))                

from sys import stdin

test = stdin.readlines()
inf = float('infinity')

q = int(test[0])
k = 1
for _ in range(q):
    n = int(test[k])

    h, c = [], []
    for j in range(k+1, k+n+1):
        x, y = list(map(int, test[j].split()))
        h.append(x)
        c.append(y)

    dp = [[0, c[0], 2 * c[0]]] + [[0, 0, 0] for i in range(n-1)]

    for i in range(1, n):
        for j in range(3):
            x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
            y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
            z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
            dp[i][j] = min(x, y, z) + j * c[i]

    print(min(dp[n - 1]))

    k += n + 1


from sys import stdin

test = stdin.readlines()
inf = float('infinity')

q = int(test[0])
k = 1
for _ in range(q):
    n = int(test[k])

    h, c = [], []
    for j in range(k+1, k+n+1):
        x, y = list(map(int, test[j].split()))
        h.append(x)
        c.append(y)

    dp = [[0, c[0], 2 * c[0]]]
    dp += [[0, 0, 0] for i in range(n-1)]

    for i in range(1, n):
        for j in range(3):
            x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
            y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
            z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
            dp[i][j] = min(x, y, z) + j * c[i]

    print(min(dp[n - 1]))

    k += n + 1


from sys import stdin

test = stdin.readlines()
inf = float('infinity')

q = int(test[0])
k = 1
for _ in range(q):
    n = int(test[k])

    h, c = [], []
    for j in range(k+1, k+n+1):
        x, y = list(map(int, test[j].split()))
        h.append(x)
        c.append(y)

    dp = [[0, 0, 0] for i in range(n)]
    dp[0] = [0, c[0], 2 * c[0]]

    for i in range(1, n):
        for j in range(3):
            x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
            y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
            z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
            dp[i][j] = min(x, y, z) + j * c[i]

    print(min(dp[n - 1]))

    k += n + 1


from sys import stdin

test = stdin.readlines()
inf = float('infinity')

q = int(test[0])
k = 1
for _ in range(q):
    n = int(test[k])

    h, c = [0]*n, [0]*n
    for j in range(k+1, k+n+1):
        h[j-k-1], c[j-k-1] = list(map(int, test[j].split()))

    dp = [[0, c[0], 2 * c[0]]] + [[0, 0, 0] for i in range(n-1)]

    for i in range(1, n):
        for j in range(3):
            x = (dp[i - 1][0] if h[i - 1] != h[i] + j else inf)
            y = (dp[i - 1][1] if h[i - 1] + 1 != h[i] + j else inf)
            z = (dp[i - 1][2] if h[i - 1] + 2 != h[i] + j else inf)
            dp[i][j] = min(x, y, z) + j * c[i]

    print(min(dp[n - 1]))

    k += n + 1


import sys
input = sys.stdin.readline
t=int(input()) 
for rainbow in range(t):
    n=int(input())
    f=[tuple(map(int,input().split())) for i in range(n)]
    dp=[0]*n;dp1=[0]*n;dp2=[0]*n
    dp1[0]=f[0][1];dp2[0]=f[0][1]*2
    for i in range(1,n):
        a=f[i-1][0]
        b=f[i][0]
        if b==a:
            dp[i]=min(dp1[i-1],dp2[i-1])
            dp1[i]=f[i][1]+min(dp[i-1],dp2[i-1])
            dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1])
        elif b==a+1:
            dp[i]=min(dp[i-1],dp2[i-1])
            dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1])
            dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1],dp2[i-1])
        elif b==a+2:
            dp[i]=min(dp[i-1],dp1[i-1])
            dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1],dp2[i-1])
            dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1],dp2[i-1])
        elif b==a-1:
            dp[i]=min(dp[i-1],dp1[i-1],dp2[i-1])
            dp1[i]=f[i][1]+min(dp1[i-1],dp2[i-1])
            dp2[i]=f[i][1]*2+min(dp[i-1],dp2[i-1])
        elif b==a-2:
            dp[i]=min(dp[i-1],dp1[i-1],dp2[i-1])
            dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1],dp2[i-1])
            dp2[i]=f[i][1]*2+min(dp1[i-1],dp2[i-1])
        else:
            dp[i]=min(dp[i-1],dp1[i-1],dp2[i-1])
            dp1[i]=f[i][1]+min(dp[i-1],dp1[i-1],dp2[i-1])
            dp2[i]=f[i][1]*2+min(dp[i-1],dp1[i-1],dp2[i-1])
    print(min(dp[n-1],dp1[n-1],dp2[n-1]))

"""
NTC here
"""
from sys import setcheckinterval, stdin, setrecursionlimit
setcheckinterval(1000)
setrecursionlimit(10**7)
 
# print("Case #{}: {} {}".format(i, n + m, n * m))
 
 
def iin(): return int(stdin.readline())
 
 
def lin(): return list(map(int, stdin.readline().split()))

for _ in range(iin()):
    n=iin()
    fence=[lin() for i in range(n)]
    dp=[[0,j,2*j] for i,j in fence]
    for i in range(1,n):
        for j in range(3):
            dp[i][j]+= min([dp[i-1][k] for k in range(3) if fence[i-1][0]+k!=fence[i][0]+j])
    #print(*dp)
    print(min(dp[-1]))

# -*- coding: utf-8 -*-
import bisect
import heapq
import math
# import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


# @mt
def slv(N, AB):
    
    memo = [0, AB[0][1], AB[0][1]*2]

    for i in range(1, N):
        a, b = AB[i]
        a1, _ = AB[i-1]
        memo2 = [0] * 3
        for j in range(3):
            tmp = 1e+1000
            for k in range(3):
                if a + j != a1 + k:
                    tmp = min(tmp, memo[k])
            memo2[j] = tmp + j * b
        memo = memo2
    return min(memo)


def main():
    Q = read_int()
    for _ in range(Q):
        N = read_int()
        AB = [read_int_n() for _ in range(N)]
        print(slv(N, AB))

    # N = 100
    # AB = [[1000000000, 1000000000] for _ in range(N)]
    # print(slv(N, AB))

def __starting_point():
    main()

__starting_point()

import sys


for q in range(int(input())):
    n = int(sys.stdin.readline())
    data = []
    for i in range(n):
        data.append([int(j) for j in sys.stdin.readline().split()])
    dp = [[0, data[0][1], 2 * data[0][1]]]
    for i in range(1, n):
        a, b = data[i]
        lasta = data[i-1][0]
        l = dp[-1]

        vals = [(lasta + j) for j in range(3)]
        ans = [0]*3
        for j in range(3):
            w = a + j
            x = [ l[k] for k in range(3) if lasta + k != w ]
            # print(w, dp[-1], x)
            ans[j] = j*b + min(x)
        dp.append(ans)
    print(min(dp[-1]))
    # print(dp)


from math import inf as inf
import sys
for _ in range(int(input())):
    n=int(sys.stdin.readline())
    dp = [[inf,inf,inf] for _ in range(n+1)]
    arr = []
    for i in range(n):
        arr.append(list(map(int,sys.stdin.readline().split())))
    dp[0] = [0,arr[0][1],arr[0][1]*2]
    
    for i in range(1,n):
        for j in range(3):
            for k in range(3):
                if arr[i][0]+j!=arr[i-1][0]+k:
                    dp[i][j] = min(dp[i][j],dp[i-1][k]+j*arr[i][1])
    print(min(dp[n-1]))



import sys, math
INF = float("inf")
def main():
    def input():
        return sys.stdin.readline()[:-1]
    q = int(input())
    for _ in range(q):
        n = int(input())
        w = [list(map(int,input().split())) for k in range(n)]
        dp = [[INF,INF,INF] for k in range(n)]
        dp[0] = [0,w[0][1],2*w[0][1]]
        for k in range(1,n):
            for l in range(3):
                for m in range(3):
                    if w[k-1][0]+l != w[k][0]+m:
                        dp[k][m] = min(dp[k][m], dp[k-1][l] + w[k][1]*m)
        print(min(dp[n-1]))
def __starting_point():
    main()

__starting_point()