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: MEDIUM_HARD

Solutions:
def main():
	import sys
	input = sys.stdin.readline
	query = int(input())
	for q in range(query):
		n = int(input())
		f = []
		for _ in range(n):
			f.append(list(map(int, input().split())))
		dp = [[0, f[0][1], f[0][1] * 2]]
		for i in range(1, n):
			if f[i][0] == f[i - 1][0]:
				dp1 = min(dp[i - 1][1], dp[i - 1][2])
				dp2 = min(dp[i - 1][0], dp[i - 1][2]) + f[i][1]
				dp3 = min(dp[i - 1][0], dp[i - 1][1]) + f[i][1] * 2
			elif f[i][0] == f[i - 1][0] - 1:
				dp1 = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2])
				dp2 = min(dp[i - 1][1], dp[i - 1][2]) + f[i][1]
				dp3 = min(dp[i - 1][0], dp[i - 1][2]) + f[i][1] * 2
			elif f[i][0] == f[i - 1][0] - 2:
				dp1 = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2])
				dp2 = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + f[i][1]
				dp3 = min(dp[i - 1][1], dp[i - 1][2]) + f[i][1] * 2
			elif f[i][0] == f[i - 1][0] + 1:
				dp1 = min(dp[i - 1][0], dp[i - 1][2])
				dp2 = min(dp[i - 1][0], dp[i - 1][1]) + f[i][1]
				dp3 = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + f[i][1] * 2
			elif f[i][0] == f[i - 1][0] + 2:
				dp1 = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2])
				dp2 = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + f[i][1]
				dp3 = min(dp[i - 1][1], dp[i - 1][2]) + f[i][1] * 2
			else:
				dp1 = min(dp[i - 1][0], dp[i - 1][1])
				dp2 = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + f[i][1]
				dp3 = min(dp[i - 1][1], dp[i - 1][1], dp[i - 1][2]) + f[i][1] * 2
			dp.append([dp1, dp2, dp3])
		print(min(dp[-1]))
main()


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


import sys
input = sys.stdin.readline

def minCost(fences, costs):
	n = len(fences)
	cur = [0, costs[0], 2 * costs[0]]
	for i in range(1, n):
		nxt = [float('inf'), float('inf'), float('inf')]
		for add in range(3):
			mi = float('inf')
			for x in range(3):
				if add + fences[i] != x + fences[i - 1]:
					mi = min(mi, cur[x])
			nxt[add] = add * costs[i] + mi
		cur = nxt
	return min(cur)

def main():
	lq = int(input())
	for _ in range(lq):
		n = int(input())
		fences = []
		costs = []
		for _ in range(n):
			(a, b) = input().split()
			fences.append(int(a))
			costs.append(int(b))
		print(minCost(fences, costs))
main()


import bisect
import heapq
import math
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

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 = 1e309
			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))

def __starting_point():
	main()
__starting_point()


import sys

def input():
	return sys.stdin.readline().rstrip()
INF = 1 << 128

def slv():
	n = int(input())
	a = [0] * n
	b = [0] * n
	for i in range(n):
		(h, c) = map(int, input().split())
		a[i] = h
		b[i] = c
	dp = [[INF] * 5 for i in range(n + 1)]
	for i in range(5):
		dp[1][i] = b[0] * i
	for i in range(1, n):
		prev_height = a[i - 1]
		(height, cost) = (a[i], b[i])
		for x in range(5):
			for y in range(5):
				if prev_height + x != height + y:
					dp[i + 1][y] = min(dp[i + 1][y], dp[i][x] + cost * y)
	ans = min((dp[n][i] for i in range(5)))
	print(ans)
	return

def main():
	t = int(input())
	for i in range(t):
		slv()
	return
main()


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()


from sys import stdin
from math import inf
q = int(stdin.readline())
for _ in range(q):
	n = int(stdin.readline())
	dp = [[inf for i in range(3)] for j in range(n)]
	p = 0
	for i in range(n):
		(a, b) = map(int, stdin.readline().split())
		if i == 0:
			dp[i][0] = 0
			dp[i][1] = b
			dp[i][2] = 2 * b
		else:
			if p != a:
				dp[i][0] = min(dp[i][0], dp[i - 1][0])
			if p + 1 != a:
				dp[i][0] = min(dp[i][0], dp[i - 1][1])
			if p + 2 != a:
				dp[i][0] = min(dp[i][0], dp[i - 1][2])
			if p != a + 1:
				dp[i][1] = min(dp[i][1], dp[i - 1][0] + b)
			if p + 1 != a + 1:
				dp[i][1] = min(dp[i][1], dp[i - 1][1] + b)
			if p + 2 != a + 1:
				dp[i][1] = min(dp[i][1], dp[i - 1][2] + b)
			if p != a + 2:
				dp[i][2] = min(dp[i][2], dp[i - 1][0] + 2 * b)
			if p + 1 != a + 2:
				dp[i][2] = min(dp[i][2], dp[i - 1][1] + 2 * b)
			if p + 2 != a + 2:
				dp[i][2] = min(dp[i][2], dp[i - 1][2] + 2 * b)
		p = a
	print(min(dp[n - 1]))


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	n = int(input())
	h = []
	c = []
	for i in range(n):
		(a, b) = map(int, input().split())
		h.append(a)
		c.append(b)
	dp = [[0, 0, 0, 0] for i in range(n + 1)]
	dp[1][1] = 0
	dp[1][2] = c[0]
	dp[1][3] = c[0] * 2
	for i in range(2, n + 1):
		if h[i - 1] == h[i - 2]:
			dp[i][1] = min(dp[i - 1][2], dp[i - 1][3])
			dp[i][2] = min(dp[i - 1][1], dp[i - 1][3]) + c[i - 1]
			dp[i][3] = min(dp[i - 1][1], dp[i - 1][2]) + 2 * c[i - 1]
		elif h[i - 1] + 1 == h[i - 2]:
			dp[i][1] = min(dp[i - 1][2], dp[i - 1][1], dp[i - 1][3])
			dp[i][2] = min(dp[i - 1][2], dp[i - 1][3]) + c[i - 1]
			dp[i][3] = min(dp[i - 1][2], dp[i - 1][1]) + 2 * c[i - 1]
		elif h[i - 1] - 1 == h[i - 2]:
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][3])
			dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + c[i - 1]
			dp[i][3] = min(dp[i - 1][1], dp[i - 1][2], dp[i - 1][3]) + 2 * c[i - 1]
		else:
			x = min(dp[i - 1][1], dp[i - 1][2], dp[i - 1][3])
			dp[i][1] = x
			dp[i][2] = x + c[i - 1]
			dp[i][3] = x + 2 * c[i - 1]
	print(min(dp[-1][1], dp[-1][2], dp[-1][3]))


import os, sys
from io import BytesIO, IOBase
from math import inf

def main():
	for _ in range(int(input())):
		n = int(input())
		(arr, cost) = ([], [])
		for _ in range(n):
			(a, b) = map(int, input().split())
			arr.append(a)
			cost.append(b)
		dp = [[inf, inf, inf] for _ in range(n)]
		dp[0] = [0, cost[0], 2 * cost[0]]
		for i in range(1, n):
			if arr[i] != arr[i - 1]:
				dp[i][0] = dp[i - 1][0]
				dp[i][1] = dp[i - 1][1] + cost[i]
				dp[i][2] = dp[i - 1][2] + 2 * cost[i]
			if arr[i] != arr[i - 1] + 1:
				dp[i][0] = min(dp[i][0], dp[i - 1][1])
				dp[i][1] = min(dp[i][1], dp[i - 1][2] + cost[i])
			if arr[i] != arr[i - 1] + 2:
				dp[i][0] = min(dp[i][0], dp[i - 1][2])
			if arr[i] + 1 != arr[i - 1]:
				dp[i][1] = min(dp[i][1], dp[i - 1][0] + cost[i])
				dp[i][2] = min(dp[i][2], dp[i - 1][1] + 2 * cost[i])
			if arr[i] + 2 != arr[i - 1]:
				dp[i][2] = min(dp[i][2], dp[i - 1][0] + 2 * cost[i])
		print(min(dp[-1]))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
output = []
INF = int(1e+18)
a = [0] * int(300000.0 + 5)
p = [0] * int(300000.0 + 5)
q = int(input())
dp = [[INF, INF, INF] for i in range(int(300000.0 + 5))]
for Case in range(q):
	n = int(input())
	i = 0
	while i < n:
		(a[i], p[i]) = map(int, input().split())
		i += 1
	for i in range(3):
		dp[0][i] = p[0] * i
	for i in range(1, n):
		for s1 in range(3):
			dp[i][s1] = min((dp[i - 1][s2] + s1 * p[i] for s2 in range(3) if a[i - 1] + s2 != a[i] + s1))
	output.append(min((dp[n - 1][i] for i in range(3))))
print(*output, sep='\n')


import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	n = int(input())
	a = [0] * n
	b = [0] * n
	for i in range(n):
		(a[i], b[i]) = map(int, input().split())
	dp = [[10 ** 18] * 3 for i in range(n)]
	dp[0][0] = 0
	dp[0][1] = b[0]
	dp[0][2] = b[0] * 2
	for i in range(1, n):
		for j in range(3):
			for k in range(3):
				if a[i] + k != a[i - 1] + j:
					dp[i][k] = min(dp[i][k], dp[i - 1][j] + b[i] * k)
	print(min(dp[-1]))


import sys
readline = sys.stdin.readline
write = sys.stdout.write
q = int(readline())
INF = 10 ** 18
z = [INF] * 3
(*r,) = range(3)
for _ in range(q):
	N = int(readline())
	prv = -100
	S = [0, INF, INF]
	T = [0, 0, 0]
	for i in range(N):
		(a, b) = map(int, readline().split())
		T[:] = z
		for p in r:
			for q in r:
				if prv + p == a + q:
					continue
				T[q] = min(T[q], S[p] + q * b)
		(S, T) = (T, S)
		prv = a
	write('%d\n' % min(S))


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]))
main()


import sys
import math, bisect
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	n = I()
	t = float('inf')
	dp = [[t, t, t] * 3 for i in range(n + 1)]
	(dp[0][0], dp[0][1], dp[0][2]) = (0, 0, 0)
	(A, B) = ([t], [t])
	for i in range(n):
		(a, b) = neo()
		A += [a]
		B += [b]
	for i in range(1, n + 1):
		for j in range(3):
			for k in range(3):
				if A[i - 1] + k != A[i] + j:
					dp[i][j] = min(dp[i][j], B[i] * j + dp[i - 1][k])
	print(min(dp[-1]))


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


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)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = [0 for x in range(n)]
	b = [0 for x in range(n)]
	dp = [[0, 0, 0] for x in range(n)]
	(u, v) = map(int, sys.stdin.readline().split())
	(a[0], b[0]) = (u, v)
	dp[0] = [0, v, 2 * v]
	for i in range(1, n):
		(u, v) = map(int, sys.stdin.readline().split())
		(a[i], b[i]) = (u, v)
		if a[i] == a[i - 1]:
			dp[i][0] = min(dp[i - 1][1], dp[i - 1][2])
			dp[i][1] = v + min(dp[i - 1][0], dp[i - 1][2])
			dp[i][2] = 2 * v + min(dp[i - 1][0], dp[i - 1][1])
		elif abs(a[i] - a[i - 1]) > 2:
			dp[i] = [min(dp[i - 1]), min(dp[i - 1]) + v, min(dp[i - 1]) + 2 * v]
		elif a[i] - a[i - 1] == 2:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
			dp[i][1] = min(dp[i - 1]) + v
			dp[i][2] = min(dp[i - 1]) + 2 * v
		elif a[i] - a[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]) + v
			dp[i][2] = min(dp[i - 1]) + 2 * v
		elif a[i] - a[i - 1] == -1:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + v
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + 2 * v
		else:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1]) + v
			dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + 2 * v
	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) = 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, 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)
threading.stack_size(2 ** 27)
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):
	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):
	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:
			x += b
		b = b // 2
	return x
TestCases = True
fac_warm_up_size = 10 ** 5 + 100
optimise_for_recursion = False

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(min(dp[-1]))
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 = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
for _ in range(int(input())):
	n = int(input())
	a = []
	b = []
	for __ in range(n):
		(c1, c2) = map(int, input().split())
		if __ == 0:
			a = [c1, c1 + 1, c1 + 2]
			b = [0, c2, 2 * c2]
		else:
			c = [c1, c1 + 1, c1 + 2]
			e = [float('inf'), float('inf'), float('inf')]
			for i in range(3):
				for j in range(3):
					if c[i] != a[j]:
						e[i] = min(e[i], b[j] + i * c2)
			a = c
			b = e
	print(min(b))


import sys
input = sys.stdin.readline
q = int(input())
INF = 10 ** 18
for _ in range(q):
	n = int(input())
	info = [list(map(int, input().split())) for i in range(n)]
	dp = [[INF] * 3 for i in range(n + 1)]
	dp[0][0] = 0
	dp[0][1] = 0
	dp[0][2] = 0
	for i in range(n):
		b = info[i][1]
		for j in range(3):
			for k in range(3):
				if i == 0 or info[i][0] + j != info[i - 1][0] + k:
					dp[i + 1][j] = min(dp[i + 1][j], dp[i][k] + j * b)
	print(min(dp[-1]))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	for j in range(int(input())):
		n = int(input())
		vals = []
		price = []
		pairs = []
		dict1 = {}
		for s in range(n):
			(a, b) = map(int, input().split())
			vals.append(a)
			price.append(b)
		dp = [0, price[0], 2 * price[0]]
		for s in range(1, n):
			dp0 = []
			for b in range(3):
				new = vals[s] + b
				p = b * price[s]
				minval = 10 ** 18
				for i in range(3):
					if new != vals[s - 1] + i:
						newp = dp[i] + p
						if newp < minval:
							minval = newp
				dp0.append(minval)
			dp = dp0
		print(min(dp))
main()


from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(10 ** 4)
PI = float('inf')

def fn(pos, last_inc):
	if pos == n:
		return 0
	inc = exc = PI
	if (pos, last_inc) in dp:
		return dp[pos, last_inc]
	if pos == 0:
		inc = min(lc[0][1] + fn(pos + 1, 1), 2 * lc[0][1] + fn(pos + 1, 2))
		exc = fn(pos + 1, 0)
	else:
		prev = lc[pos - 1][0] + last_inc
		if lc[pos][0] + 1 != prev:
			inc = min(lc[pos][1] + fn(pos + 1, 1), inc)
		if lc[pos][0] + 2 != prev:
			inc = min(inc, 2 * lc[pos][1] + fn(pos + 1, 2))
		if lc[pos][0] != prev:
			exc = fn(pos + 1, 0)
	dp[pos, last_inc] = min(inc, exc)
	return dp[pos, last_inc]
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	lc = []
	for _ in range(n):
		(ll, cc) = list(map(int, stdin.readline().split()))
		lc += [[ll, cc]]
	dp = [[PI for _ in range(3)] for _ in range(n)]
	dp[0][0] = 0
	dp[0][1] = lc[0][1]
	dp[0][2] = 2 * lc[0][1]
	for i in range(1, n):
		for j in range(3):
			if lc[i][0] + 0 != lc[i - 1][0] + j:
				dp[i][0] = min(dp[i][0], dp[i - 1][j] + 0 * lc[i][1])
			if lc[i][0] + 1 != lc[i - 1][0] + j:
				dp[i][1] = min(dp[i][1], dp[i - 1][j] + 1 * lc[i][1])
			if lc[i][0] + 2 != lc[i - 1][0] + j:
				dp[i][2] = min(dp[i][2], dp[i - 1][j] + 2 * lc[i][1])
	print(min(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')

def main():
	for _ in range(N()):
		n = N()
		arr = [RLL() for _ in range(n)]
		dp = [[INF] * 3 for _ in range(n + 1)]
		dp[0] = [0, 0, 0]
		dp[1] = [arr[0][1] * i for i in range(3)]
		for i in range(2, n + 1):
			for j in range(3):
				for k in range(3):
					if arr[i - 1][0] + j != arr[i - 2][0] + k:
						dp[i][j] = min(dp[i - 1][k] + j * arr[i - 1][1], dp[i][j])
		print(min(dp[-1]))
main()


import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__

def solve():
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(u, v) = list(map(int, input().split(' ')))
		a.append(u)
		b.append(v)
	dp = [[0, 0, 0] for _ in range(n)]
	dp[0] = [0, b[0], b[0] * 2]
	for i in range(1, n):
		dp[i] = [b[i] * j + min([dp[i - 1][k] for k in range(3) if a[i] + j != a[i - 1] + k]) for j in range(3)]
	return min(*dp[-1])
t = int(input())
for _ in range(t):
	print(solve())


from typing import Iterator, Callable
import sys
input = sys.stdin.readline

def read_inp() -> 'Iterator[tuple[int, int]]':
	for _ in range(int(input())):
		yield map(int, input().split())
	yield (None, None)

def get_answer(get_next: 'Callable[[], tuple[int, int]]') -> 'int':
	(a1, b1) = get_next()
	dp_last_row = {a1: 0, a1 + 1: b1, a1 + 2: b1 * 2}
	while True:
		(a_i, b_i) = get_next()
		if a_i is None:
			break
		now = {}
		for diff in range(3):
			for (prev_last, prev_cost) in dp_last_row.items():
				last = a_i + diff
				if last == prev_last:
					continue
				cur = prev_cost + b_i * diff
				if last not in now or now[last] > cur:
					now[last] = cur
		dp_last_row = now
	return min(dp_last_row.items(), key=lambda x: x[1])[1]
for _ in range(int(input())):
	data = read_inp()
	print(get_answer(lambda : next(data)))


import sys
q = int(sys.stdin.readline())
for i in range(q):
	n = int(sys.stdin.readline())
	(newBoard, newCost) = map(int, sys.stdin.readline().split())
	lastNet = [0, newCost, newCost * 2, newBoard]
	newNet = [0, 0, 0, 0]
	for i in range(n - 1):
		(newBoard, newCost) = map(int, sys.stdin.readline().split())
		for currentChange in range(3):
			nodeChange = []
			for prevChange in range(3):
				if lastNet[3] + prevChange != newBoard + currentChange:
					nodeChange.append(lastNet[prevChange] + currentChange * newCost)
			newNet[currentChange] = min(nodeChange)
		newNet[3] = newBoard
		lastNet = newNet.copy()
	print(min(lastNet[:3]))


import sys, os
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
from itertools import permutations
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))
MOD = 10 ** 9 + 7

def f(n, arr):
	m = len(n)
	dp = [[[0] * 10 for j in range(2)] for i in range(m)]
	ans = int(n)
	for i in range(m - 1, -1, -1):
		for j in range(2):
			for p in range([10, int(n[i]) + 1][j]):
				dp[i][j][p] += 1 + (dp[i + 1][p == int(n[i]) and j][p] if i + 1 <= m - 1 else 1)
				f = 0
				for k in range(10):
					if dp[i][j][k] == arr[k]:
						f |= 1
				ans -= f
			for x in dp:
				for y in range(2):
					print(y, x[y])
	return ans

def main():
	for _ in range(int(input())):
		n = int(input())
		(a, b) = ([], [])
		dp = [[10 ** 18 + 1] * 3 for i in range(n)]
		(la, lb) = read()
		for i in range(3):
			dp[0][i] = lb * i
		for i in range(1, n):
			(a, b) = read()
			for j in range(3):
				t = a + j
				for k in range(3):
					if t == la + k:
						dp[i][j] = j * b + min(dp[i - 1][(k + 1) % 3], dp[i - 1][(k + 2) % 3])
					else:
						dp[i][j] = min(dp[i][j], j * b + dp[i - 1][k])
			(la, lb) = (a, b)
		print(min(dp[n - 1]))
main()


import sys, os, io
from sys import stdin
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(l, r) = [int(x) for x in input().split()]
		a.append(l)
		b.append(r)
	dp = [[float('inf'), float('inf'), float('inf')] for i in range(n)]
	dp[0][0] = 0
	dp[0][1] = b[0]
	dp[0][2] = b[0] * 2
	for i in range(1, n):
		for j1 in range(3):
			for j2 in range(3):
				if a[i] + j1 != a[i - 1] + j2:
					dp[i][j1] = min(dp[i][j1], dp[i - 1][j2] + j1 * b[i])
	print(min(dp[-1]))


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]))


import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		(a, b) = map(int, input().split())
		arr.append([a, b])
	dp = [[999999999999999999999999999 for i in range(3)] for j in range(n)]
	dp[0][0] = 0
	dp[0][1] = arr[0][1]
	dp[0][2] = arr[0][1] * 2
	dpp = [[0 for i in range(3)] for j in range(n)]
	for i in range(n):
		for j in range(3):
			dpp[i][j] = arr[i][0] + j
	j = 0
	for i in range(1, n):
		for j in range(3):
			for k in range(3):
				if dpp[i - 1][k] - dpp[i][j] == 0:
					pass
				else:
					dp[i][j] = min(dp[i][j], dp[i - 1][k] + arr[i][1] * j)
	mini = 999999999999999999999999999
	for i in range(3):
		mini = min(mini, dp[-1][i])
	print(mini)


from math import inf as inf
from math import *
from collections import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	g = float('inf')
	dp = [[g, g, g] for i in range(n + 1)]
	a = []
	for i in range(n):
		a.append(list(map(int, input().split())))
	dp[0] = [0, a[0][1], a[0][1] * 2]
	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] + a[i][1] * j)
	print(min(dp[n - 1]))


from sys import stdin
t = int(input())
for i2 in range(t):
	n = int(stdin.readline())
	x = [[0] * 2 for i in range(n)]
	dp = [[0] * 3 for i in range(n)]
	for i in range(n):
		(x[i][0], x[i][1]) = map(int, stdin.readline().split())
	for i in range(n):
		if i == 0:
			(dp[0][0], dp[0][1], dp[0][2]) = (0, x[0][1], x[0][1] * 2)
		elif x[i][0] == x[i - 1][0] + 2:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + x[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + x[i][1] * 2
		elif x[i][0] == x[i - 1][0] + 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]) + x[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + x[i][1] * 2
		elif x[i][0] == x[i - 1][0]:
			dp[i][0] = min(dp[i - 1][1], dp[i - 1][2])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + x[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + x[i][1] * 2
		elif x[i][0] == x[i - 1][0] - 1:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2])
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + x[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + x[i][1] * 2
		elif x[i][0] == x[i - 1][0] - 2:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + x[i][1]
			dp[i][2] = min(dp[i - 1][1], dp[i - 2][2]) + x[i][1] * 2
		else:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + x[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1], dp[i - 1][2]) + x[i][1] * 2
	print(min(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]))


def main():
	q = int(input())
	allans = []
	for _ in range(q):
		n = int(input())
		a = []
		b = []
		for __ in range(n):
			(aa, bb) = readIntArr()
			a.append(aa)
			b.append(bb)
		dp = [[inf, inf, inf] for __ 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 j1 in range(3):
				for j2 in range(3):
					if a[i - 1] + j1 != a[i] + j2:
						dp[i][j2] = min(dp[i][j2], j2 * b[i] + dp[i - 1][j1])
		ans = min(dp[n - 1][0], dp[n - 1][1], dp[n - 1][2])
		allans.append(ans)
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(i, j):
	print('? {} {}'.format(i, j))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(' '.join([str(x) for x in ans])))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	n = int(input())
	ans = 0
	(a, b) = map(int, input().split())
	cur = a
	A = [0, b, 2 * b]
	for i in range(n - 1):
		a = cur
		(na, nb) = 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))


from sys import stdin, stdout

def check(lim, arr):
	if int(arr[0]) < lim or int(arr[1]) < lim:
		return False
	return max(0, int(arr[0]) - lim) + max(0, int(arr[1]) - lim) + int(arr[2]) >= lim
q = int(stdin.readline())
while q > 0:
	q = q - 1
	n = int(stdin.readline())
	a = []
	b = []
	a.append(0)
	b.append(0)
	for i in range(n):
		arr = stdin.readline().split(' ')
		a.append(int(arr[0]))
		b.append(int(arr[1]))
	dp = [1e+18] * (n + 1)
	for i in range(n + 1):
		dp[i] = [1e+18] * 4
	for i in range(4):
		dp[0][i] = 0
	for i in range(0, n):
		for j in range(0, 4):
			for k in range(0, 4):
				if a[i] + int(j) != a[i + 1] + int(k):
					dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + k * b[i + 1])
	ans = 1e+18
	for i in range(4):
		ans = min(ans, dp[n][i])
	ans = int(ans)
	stdout.write(str(ans) + '\n')


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


import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
for _ in range(int(input())):
	n = int(input())
	a = [tuple(map(int, input().split())) for _ in range(n)]
	dp = [[0, 0, 0] for _ in range(n)]
	dp[0] = [0, a[0][1], a[0][1] * 2]
	for i in range(1, n):
		dp[i] = [a[i][1] * y + min((dp[i - 1][x] for x in range(3) if a[i - 1][0] + x != a[i][0] + y)) for y in range(3)]
	out.append(min(dp[-1]))
print(*out, sep='\n')


def main():
	import sys
	input = sys.stdin.readline
	q = int(input())
	for _ in range(q):
		N = int(input())
		A = [-100]
		B = [0]
		for _ in range(N):
			(a, b) = map(int, input().split())
			A.append(a)
			B.append(b)
		inf = 10 ** 20
		dp = [[inf] * 3 for _ in range(N + 1)]
		dp[0][0] = 0
		for i in range(N):
			for add in range(3):
				for add_next in range(3):
					if A[i] + add != A[i + 1] + add_next:
						dp[i + 1][add_next] = min(dp[i + 1][add_next], dp[i][add] + B[i + 1] * add_next)
		print(min(dp[-1]))
main()


import sys
from math import ceil
q = int(sys.stdin.readline())
while q:
	q -= 1
	F = int(sys.stdin.readline())
	a = [[0, 0] for _ in range(F)]
	for i in range(F):
		(a[i][0], a[i][1]) = map(int, sys.stdin.readline().rstrip().split())
	dp = [[0, 0, 0] for _ in range(F)]
	dp[0] = [0, a[0][1], 2 * a[0][1]]
	for i in range(1, F):
		diff = a[i][0] - a[i - 1][0]
		if diff == 2:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
			dp[i][1] = min(dp[i - 1]) + a[i][1]
			dp[i][2] = min(dp[i - 1]) + 2 * a[i][1]
		elif diff == 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]) + a[i][1]
			dp[i][2] = min(dp[i - 1]) + 2 * a[i][1]
		elif diff == 0:
			dp[i][0] = min(dp[i - 1][1], dp[i - 1][2])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + a[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * a[i][1]
		elif diff == -1:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + a[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + 2 * a[i][1]
		elif diff == -2:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1]) + a[i][1]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * a[i][1]
		else:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1]) + a[i][1]
			dp[i][2] = min(dp[i - 1]) + 2 * a[i][1]
	print(min(dp[-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))


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]
			ans[j] = j * b + min(x)
		dp.append(ans)
	print(min(dp[-1]))


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


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect
from functools import lru_cache

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

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

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve(k, p):
	if p == n[0]:
		return 0
	if dp[k][p] != -1:
		return dp[k][p]
	else:
		temp = 1e+20
		for i in range(3):
			if p == 0 or a[p - 1] + k != a[p] + i:
				temp = min(temp, solve(i, p + 1) + i * b[p])
		dp[k][p] = temp
		return temp
t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	b = []
	for i in range(n):
		(aa, bb) = mapin()
		a.append(aa)
		b.append(bb)
	dp = [[1e+20 for i in range(n)] for j in range(3)]
	dp[0][0] = b[0] * 0
	dp[1][0] = b[0] * 1
	dp[2][0] = b[0] * 2
	for i in range(1, n):
		for j in range(3):
			for k in range(3):
				if a[i] + j != a[i - 1] + k:
					dp[j][i] = min(dp[j][i], dp[k][i - 1] + j * b[i])
	ans = 1e+20
	for i in range(3):
		ans = min(ans, dp[i][-1])
	print(ans)


import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
q = int(input())
for _ in range(q):
	n = int(input())
	dp = [[0, 0, 0] for i in range(n + 1)]
	it = [-1] * n
	for __ in range(n):
		it[__] = list(map(int, input().split()))
	it.append([-100, -100])
	for i in range(n):
		for j in range(3):
			dp[i][j] = it[i][1] * j + min([dp[i - 1][k] for k in range(3) if it[i - 1][0] + k != it[i][0] + j])
	print(min(dp[-2]))


from sys import *
from math import *
for i in range(int(stdin.readline())):
	n = int(stdin.readline())
	B = []
	for j in range(n):
		(a, b) = map(int, stdin.readline().split())
		B += [[a, b]]
	ans = [0, B[0][1], 2 * B[0][1]]
	for j in range(1, len(B)):
		tAns = [inf] * 3
		for c in range(3):
			for p in range(3):
				if B[j][0] + c != B[j - 1][0] + p:
					tAns[c] = min(tAns[c], ans[p] + B[j][1] * c)
		ans = tAns
	print(min(ans))


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.buffer.readline
INF = 10 ** 18 + 1

def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		A = [0] * n
		B = [0] * n
		for i in range(n):
			(a, b) = map(int, input().split())
			A[i] = a
			B[i] = b
		dp = [[INF] * 3 for _ in range(n + 1)]
		dp[0][0] = 0
		for i in range(n):
			for j in range(3):
				for k in range(3):
					if i != 0:
						if A[i - 1] + j != A[i] + k:
							dp[i + 1][k] = min(dp[i][j] + B[i] * k, dp[i + 1][k])
					else:
						dp[i + 1][k] = min(dp[i][j] + B[i] * k, dp[i + 1][k])
		print(min(dp[-1]))
main()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
for _ in range(int(input())):
	n = int(input())
	a = list()
	b = list()
	for i in range(n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	dp = [[0 for j in range(3)] 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 j in range(3):
			mn = 10 ** 18
			for k in range(3):
				if a[i] + j != a[i - 1] + k:
					mn = min(mn, dp[i - 1][k])
			dp[i][j] = mn + b[i] * j
	print(min(dp[n - 1]))


import sys
import math as mt
input = sys.stdin.buffer.readline
t = int(input())
for ___ in range(t):
	l = []
	n = int(input())
	for _ in range(n):
		(a, b) = map(int, input().split())
		l.append([a, b])
	ans = 0
	dp = [[0 for j in range(3)] for i in range(n + 1)]
	dp[0][0] = 0
	dp[0][1] = l[0][1]
	dp[0][2] = 2 * l[0][1]
	for i in range(1, n):
		for j in range(3):
			mini = 10 ** 18
			for x in range(3):
				if l[i - 1][0] + x != l[i][0] + j:
					mini = min(mini, dp[i - 1][x] + l[i][1] * j)
			dp[i][j] = mini
	ans = 10 ** 18
	for i in range(3):
		ans = min(ans, dp[n - 1][i])
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(a, b) = ([], [])
	n = int(input())
	for i in range(n):
		(x, y) = [int(x) for x in input().split()]
		a.append(x)
		b.append(y)
	dp = [[sys.maxsize] * 3 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 j in range(3):
			dp[i][j] = b[i] * j + min((dp[i - 1][k] for k in range(3) if a[i] + j != a[i - 1] + k))
	print(min(dp[-1]))


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
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
q = int(input())
for _ in range(q):
	n = int(input())
	a = [None] * n
	b = [None] * n
	for i in range(n):
		(ai, bi) = map(int, input().split())
		a[i] = ai
		b[i] = bi
	dp = [[0] * 3 for _ in range(n)]
	dp[0][1] = b[0]
	dp[0][2] = 2 * b[0]
	for i in range(1, n):
		for shift in range(3):
			minAdd = float('inf')
			for prev_shift in range(3):
				if a[i - 1] + prev_shift == a[i] + shift:
					continue
				minAdd = min(minAdd, dp[i - 1][prev_shift])
			dp[i][shift] += shift * b[i] + minAdd
	ans = min((dp[-1][shift] for shift in range(3)))
	print(ans)


from sys import stdin
from math import ceil, log
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
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
source = input

def great_fence(values_and_costs):
	(value_previous, cost_first) = next(values_and_costs)
	cost_to_make_higher = (0, cost_first, cost_first * 2)
	for (value, cost) in values_and_costs:
		previous_costs = cost_to_make_higher
		m = min(previous_costs)
		if value == value_previous:
			new_costs = (min(previous_costs[1], previous_costs[2]), min(previous_costs[0], previous_costs[2]) + cost, min(previous_costs[0], previous_costs[1]) + 2 * cost)
		elif value == value_previous + 1:
			new_costs = (min(previous_costs[0], previous_costs[2]), min(previous_costs[0], previous_costs[1]) + cost, m + 2 * cost)
		elif value == value_previous + 2:
			new_costs = (min(previous_costs[0], previous_costs[1]), m + cost, m + 2 * cost)
		elif value == value_previous - 1:
			new_costs = (m, min(previous_costs[1], previous_costs[2]) + cost, min(previous_costs[0], previous_costs[2]) + 2 * cost)
		elif value == value_previous - 2:
			new_costs = (m, min(previous_costs) + cost, min(previous_costs[1], previous_costs[2]) + 2 * cost)
		else:
			new_costs = (m, m + cost, m + 2 * cost)
		cost_to_make_higher = new_costs
		value_previous = value
	return min(cost_to_make_higher)
for q in range(int(source())):
	n = int(source())
	values_and_costs = (map(int, source().split()) for _ in range(n))
	print(great_fence(values_and_costs))


import sys
import math
input = sys.stdin.readline
from functools import cmp_to_key

def pi():
	return int(input())

def pl():
	return int(input(), 16)

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

def ts():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
mod = 998244353
f = []

def fact(n, m):
	global f
	f = [1 for i in range(n + 1)]
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % m

def fast_mod_exp(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b = b >> 1
	return res

def inverseMod(n, m):
	return fast_mod_exp(n, m - 2, m)

def ncr(n, r, m):
	if r == 0:
		return 1
	return f[n] * inverseMod(f[n - r], m) % m * inverseMod(f[r], m) % m

def main():
	D()

def D():
	t = pi()
	while t:
		t -= 1
		n = pi()
		h = [0 for i in range(n)]
		b = [0 for i in range(n)]
		for i in range(n):
			[x, y] = ti()
			h[i] = x
			b[i] = y
		inf = 999999999999999999999
		dp = [[0 for j in range(n)] for i in range(3)]
		dp[0][0] = 0
		dp[1][0] = b[0]
		dp[2][0] = 2 * b[0]
		for i in range(1, n):
			if h[i] == h[i - 1]:
				dp[0][i] = min(dp[1][i - 1], dp[2][i - 1])
				dp[1][i] = b[i] + min(dp[0][i - 1], dp[2][i - 1])
				dp[2][i] = 2 * b[i] + min(dp[0][i - 1], dp[1][i - 1])
			else:
				dp[0][i] = min(dp[0][i - 1], dp[1][i - 1] if 1 + h[i - 1] != h[i] else inf, dp[2][i - 1] if 2 + h[i - 1] != h[i] else inf)
				dp[1][i] = b[i] + min(dp[0][i - 1] if 1 + h[i] != h[i - 1] else inf, dp[1][i - 1], dp[2][i - 1] if 2 + h[i - 1] != 1 + h[i] else inf)
				dp[2][i] = 2 * b[i] + min(dp[0][i - 1] if 2 + h[i] != h[i - 1] else inf, dp[1][i - 1] if 2 + h[i] != 1 + h[i - 1] else inf, dp[2][i - 1])
		print(min(dp[0][n - 1], dp[1][n - 1], dp[2][n - 1]))
main()


import os
import sys
from io import BytesIO, IOBase
import math
from queue import Queue
import collections
import itertools
import bisect
import heapq
import random

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def countcon(s, i):
	c = 0
	ch = s[i]
	for i in range(i, len(s)):
		if s[i] == ch:
			c += 1
		else:
			break
	return c

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] > arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def maxfac(n):
	root = int(n ** 0.5)
	for i in range(2, root + 1):
		if n % i == 0:
			return n // i
	return n

def p2(n):
	c = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	return c

def seive(n):
	primes = [True] * (n + 1)
	primes[1] = primes[0] = False
	i = 2
	while i * i <= n:
		if primes[i] == True:
			for j in range(i * i, n + 1, i):
				primes[j] = False
		i += 1
	pr = []
	for i in range(0, n + 1):
		if primes[i]:
			pr.append(i)
	return pr

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def denofactinverse(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return pow(fac, m - 2, m)

def numofact(n, m):
	fac = 1
	for i in range(1, n + 1):
		fac = fac * i % m
	return fac

def sod(n):
	s = 0
	while n > 0:
		s += n % 10
		n //= 10
	return s
for xyz in range(0, int(input())):
	n = int(input())
	a = []
	b = []
	for i in range(0, n):
		(x, y) = map(int, input().split())
		a.append(x)
		b.append(y)
	inF = 10 ** 19
	dp = [[inF] * n for i in range(0, 3)]
	dp[0][0] = 0
	dp[1][0] = b[0]
	dp[2][0] = 2 * b[0]
	for i in range(1, n):
		if a[i - 1] != a[i]:
			dp[0][i] = min(dp[0][i], dp[0][i - 1])
		if a[i - 1] + 1 != a[i]:
			dp[0][i] = min(dp[0][i], dp[1][i - 1])
		if a[i - 1] + 2 != a[i]:
			dp[0][i] = min(dp[0][i], dp[2][i - 1])
		if a[i - 1] != a[i] + 1:
			dp[1][i] = min(dp[1][i], b[i] + dp[0][i - 1])
		if a[i - 1] + 1 != a[i] + 1:
			dp[1][i] = min(dp[1][i], b[i] + dp[1][i - 1])
		if a[i - 1] + 2 != a[i] + 1:
			dp[1][i] = min(dp[1][i], b[i] + dp[2][i - 1])
		if a[i - 1] != a[i] + 2:
			dp[2][i] = min(dp[2][i], 2 * b[i] + dp[0][i - 1])
		if a[i - 1] + 1 != a[i] + 2:
			dp[2][i] = min(dp[2][i], 2 * b[i] + dp[1][i - 1])
		if a[i - 1] + 2 != a[i] + 2:
			dp[2][i] = min(dp[2][i], 2 * b[i] + dp[2][i - 1])
	print(min(dp[0][-1], dp[1][-1], dp[2][-1]))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	count = int(input())
	(a1, b1) = map(int, input().split())
	dp_last_row = {a1 + x: b1 * x for x in range(3)}
	for _ in range(count - 1):
		(a_i, b_i) = map(int, input().split())
		now = {}
		for diff in range(3):
			cur_last = a_i + diff
			for (prev_last, prev_cost) in dp_last_row.items():
				if cur_last == prev_last:
					continue
				cur_cost = prev_cost + b_i * diff
				if cur_last not in now or now[cur_last] > cur_cost:
					now[cur_last] = cur_cost
		dp_last_row = now
	print(min(dp_last_row.values()))


import sys
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	a = []
	for _ in range(n):
		a.append(list(map(int, sys.stdin.readline().split())))
	d = [[10 ** 19, 10 ** 19, 10 ** 19] for _ in range(n)]
	d[0][0] = 0
	d[0][1] = a[0][1]
	d[0][2] = a[0][1] * 2
	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:
					d[i][j] = min(d[i - 1][k] + a[i][1] * j, d[i][j])
	print(min(d[n - 1]))


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


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


def main():
	import sys
	input = sys.stdin.readline
	q = int(input())
	for __ in [0] * q:
		n = int(input())
		a = []
		b = []
		for _ in [0] * n:
			(aa, bb) = map(int, input().split())
			a.append(aa)
			b.append(bb)
		dp = [[0, 0, 0] for i in [0] * n]
		dp[0][1] = b[0]
		dp[0][2] = 2 * b[0]
		for i in range(1, n):
			tmp0 = 10 ** 18
			tmp1 = 10 ** 18
			tmp2 = 10 ** 18
			if a[i] != a[i - 1] + 2:
				tmp0 = min(tmp0, dp[i - 1][2])
			if a[i] != a[i - 1] + 1:
				tmp0 = min(tmp0, dp[i - 1][1])
				tmp1 = min(tmp1, dp[i - 1][2] + b[i])
			if a[i] != a[i - 1]:
				tmp0 = min(tmp0, dp[i - 1][0])
				tmp1 = min(tmp1, dp[i - 1][1] + b[i])
				tmp2 = min(tmp2, dp[i - 1][2] + b[i] * 2)
			if a[i] != a[i - 1] - 1:
				tmp1 = min(tmp1, dp[i - 1][0] + b[i])
				tmp2 = min(tmp2, dp[i - 1][1] + b[i] * 2)
			if a[i] != a[i - 1] - 2:
				tmp2 = min(tmp2, dp[i - 1][0] + b[i] * 2)
			dp[i] = [tmp0, tmp1, tmp2]
		print(min(dp[-1]))
main()


import math
a = int(input())
import sys
input = sys.stdin.readline
for i in range(a):
	s = int(input())
	ans = []
	cost = []
	for i in range(s):
		(x, y) = map(int, input().split())
		ans.append(x)
		cost.append(y)
	dp = [[math.inf, math.inf, math.inf] for i in range(len(ans))]
	dp[0][0] = 0
	dp[0][1] = cost[0]
	dp[0][2] = 2 * cost[0]
	for i in range(1, len(ans)):
		for j in range(3):
			for k in range(3):
				if ans[i] + j == ans[i - 1] + k:
					continue
				else:
					dp[i][j] = min(dp[i - 1][k] + j * cost[i], dp[i][j])
	total = 0
	print(min(dp[-1]))


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	a = []
	b = []
	n = int(input())
	for i in range(n):
		(ai, bi) = [int(i) for i in input().split()]
		a.append(ai)
		b.append(bi)
	dp = [[10 ** 20, 10 ** 20, 10 ** 20] 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):
		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][0], dp[i - 1][2]) + b[i]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * b[i]
		elif a[i] == a[i - 1] - 1:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + b[i]
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + 2 * b[i]
		elif a[i] == a[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]) + b[i]
			dp[i][2] = min(dp[i - 1]) + 2 * b[i]
		elif a[i] == a[i - 1] + 2:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
			dp[i][1] = min(dp[i - 1]) + b[i]
			dp[i][2] = min(dp[i - 1]) + 2 * b[i]
		elif a[i] == a[i - 1] - 2:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1]) + b[i]
			dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + 2 * b[i]
		else:
			dp[i][0] = min(dp[i - 1])
			dp[i][1] = min(dp[i - 1]) + b[i]
			dp[i][2] = min(dp[i - 1]) + 2 * b[i]
	print(min(dp[n - 1]))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	ab = [list(map(int, input().split())) for i in range(n)]
	(a, b) = list(zip(*ab))
	a = list(a)
	dp = [[10 ** 18 for i in range(3)] for j in range(n + 1)]
	dp[1][0] = 0
	dp[1][1] = b[0]
	dp[1][2] = 2 * b[0]
	for i in range(2, n + 1):
		(ai, bi) = (a[i - 1], b[i - 1])
		mn = min(dp[i - 1])
		if ai == a[i - 2]:
			dp[i][0] = min(dp[i - 1][1], dp[i - 1][2])
			dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + bi
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + 2 * bi
		elif ai == a[i - 2] + 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]) + bi
			dp[i][2] = mn + 2 * bi
		elif ai == a[i - 2] - 1:
			dp[i][0] = mn
			dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + bi
			dp[i][2] = min(dp[i - 1][0], dp[i - 1][2]) + 2 * bi
		elif ai == a[i - 2] + 2:
			dp[i][0] = min(dp[i - 1][0], dp[i - 1][1])
			dp[i][1] = mn + bi
			dp[i][2] = mn + 2 * bi
		elif ai == a[i - 2] - 2:
			dp[i][0] = mn
			dp[i][1] = mn + bi
			dp[i][2] = min(dp[i - 1][1], dp[i - 1][2]) + 2 * bi
		else:
			for j in range(3):
				dp[i][j] = mn + bi * j
	print(min(dp[-1]))


import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
t = int(input())
results = []
for _ in range(t):
	n = int(input())
	hp = [tuple(map(int, input().split())) for _ in range(n)]
	dp = [[0, 0, 0] for _ in range(n)]
	dp[0] = [0, hp[0][1], hp[0][1] * 2]
	for i in range(1, n):
		dp[i] = [hp[i][1] * j + min([dp[i - 1][k] for k in range(3) if hp[i][0] + j != hp[i - 1][0] + k]) for j in range(3)]
	results.append(min(dp[-1]))
print(*results, sep='\n')


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 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
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]))


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	for _ in range(ii()):
		n = ii()
		a = []
		for i in range(n):
			a.append(li())
		dp = [[0, 0, 0] for i in range(n)]
		dp[0][1] = a[0][1]
		dp[0][2] = 2 * a[0][1]
		for i in range(1, n):
			for j in range(3):
				dp[i][j] = 1e+18
				for k in range(3):
					if a[i - 1][0] + k != a[i][0] + j:
						dp[i][j] = min(dp[i][j], dp[i - 1][k] + j * a[i][1])
		print(min(dp[-1]))
if file:
	if path.exists('input1.txt'):
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')
	else:
		input = sys.stdin.readline
solve()