You are given two integers $n$ and $m$. You have to construct the array $a$ of length $n$ consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly $m$ and the value $\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|$ is the maximum possible. Recall that $|x|$ is the absolute value of $x$.

In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array $a=[1, 3, 2, 5, 5, 0]$ then the value above for this array is $|1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11$. Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.

You have to answer $t$ independent test cases.


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.

The only line of the test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^9$) — the length of the array and its sum correspondingly.


-----Output-----

For each test case, print the answer — the maximum possible value of $\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|$ for the array $a$ consisting of $n$ non-negative integers with the sum $m$.


-----Example-----
Input
5
1 100
2 2
5 5
2 1000000000
1000000000 1000000000

Output
0
2
10
1000000000
2000000000



-----Note-----

In the first test case of the example, the only possible array is $[100]$ and the answer is obviously $0$.

In the second test case of the example, one of the possible arrays is $[2, 0]$ and the answer is $|2-0| = 2$.

In the third test case of the example, one of the possible arrays is $[0, 2, 0, 3, 0]$ and the answer is $|0-2| + |2-0| + |0-3| + |3-0| = 10$.

Source: codeforces | Difficulty: EASY
Tags: Greedy algorithms

Mathematics

Constructive algorithms


Solutions:
test = int(input())
for t in range(test):
	(a, b) = map(int, input().split())
	if a == 1:
		print(0)
	elif a == 2:
		print(b)
	else:
		print(2 * b)


for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n % 2 == 0:
		if n == 2:
			print(m)
		elif n > 2:
			print(2 * m)
	elif n == 1:
		print('0')
	elif n > 1:
		print(2 * m)


t = int(input())
for z in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	x = m // (n // 2)
	xt = m % (n // 2)
	if n % 2 == 0:
		ans = x * (n // 2) * 2
		if n > 2:
			ans += xt * 2
		else:
			ans += xt - x
	else:
		ans = x * (n // 2) * 2
		ans += 2 * xt
	print(ans)


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
	else:
		print(m * 2)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if a >= 3:
		print(2 * b)
	elif a == 2:
		print(b)
	elif a == 1:
		print(0)


for _ in range(int(input())):
	(N, M) = list(map(int, input().split()))
	if N == 1:
		print(0)
	elif N == 2:
		print(M)
	else:
		print(2 * M)


for _ in range(int(input())):
	(n, s) = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(s)
	else:
		print(s * 2)


for _p in range(int(input())):
	(a, b) = map(int, input().split())
	if a > 2:
		print(b * 2)
	elif a == 2:
		print(b)
	else:
		print(0)


t = int(input())
for _ in range(t):
	(n, su_m) = map(int, input().split())
	if n == 1:
		print('0')
	elif n == 2:
		print(su_m)
	else:
		print(2 * su_m)


def main():
	(n, m) = map(int, input().split())
	new_lst = []
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
t = int(input())
for i in range(t):
	main()


t = int(input())
while t:
	t -= 1
	(n, k) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


t = int(input())
for i in range(t):
	l = list(map(int, input().rstrip().split()))
	n = l[0]
	m = l[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for _ in range(int(input())):
	(m, n) = [int(s) for s in input().split()]
	if m == 1:
		print(0)
	elif m == 2:
		print(n)
	else:
		print(2 * n)


n = int(input())
for _ in range(n):
	(x, y) = map(int, input().split())
	if x == 1:
		print('0')
	elif x == 2:
		print(y)
	else:
		m = x // 2
		if y < m:
			print(2 * y)
		else:
			h = y // m
			r = y % m
			ans = h * (m - 1) * 2 + 2 * (h + r)
			print(ans)


def mua(n, m):
	if n == 1:
		print('0')
		return
	elif n == 2:
		print(m)
		return
	else:
		print(2 * m)
	return
t = int(input())
for i in range(t):
	p = input()
	t1 = list(map(int, p.split()))
	mua(t1[0], t1[1])


import sys
input = sys.stdin.readline
for t in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


testCase = int(input())
for i in range(testCase):
	(n, m) = [int(x) for x in input().split(' ')]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().rstrip().split())
	if n == 1:
		print('0')
		continue
	if n == 2:
		print(m)
		continue
	if n % 2 == 0:
		res = m * 2
	else:
		res = m * 2
	print(res)


import sys
import math
from collections import deque
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def main_function():
	from sys import stdin
	from sys import stdout
	input = stdin.readline
	print = stdout.write
	t = int(input())
	for _ in range(t):
		(n, m) = list(map(int, input().split()))
		if n == 1:
			print(f'0\n')
		elif n == 2:
			print(f'{m}\n')
		else:
			print(f'{m * 2}\n')
main_function()


t = int(input())
for _ in range(t):
	(length, sum) = map(int, input().split())
	if length == 1:
		print(0)
	elif length == 2:
		print(sum * 2 - sum // (length // 2))
	else:
		print(sum * 2)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

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

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

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

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

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
for _ in range(ii()):
	(n, m) = mi()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


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

def solve():
	(n, total) = map(lambda x: int(x), input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(total)
	else:
		print(total * 2)
main()


def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	else:
		return 2 * m
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	print(solve(n, m))


def main():
	n = int(input())
	cases = []
	for _ in range(n):
		(n, m) = [int(x) for x in input().split()]
		cases.append([n, m])
	for i in range(len(cases)):
		(n, m) = (cases[i][0], cases[i][1])
		if n == 1:
			ans = 0
			print(ans)
		elif n == 2:
			ans = m
			print(ans)
		elif n > 2:
			ans = min(2, n - 1) * m
			print(ans)
main()


from sys import stdin, stdout
import collections
import heapq
import itertools
import functools

def read_line():
	return stdin.readline().strip()

def read_int():
	return int(read_line())

def read_arr():
	return read_line().split(' ')

def read_int_arr():
	return [int(x) for x in read_arr()]

def main():
	T = read_int()
	for _ in range(T):
		(n, m) = read_int_arr()
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
main()


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
t = 1
t = int(input())
for _ in range(t):
	(n, m) = map(int, stdin.readline().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m + m)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = min(2, n - 1)
	ans = a * m
	print(ans)


N = int(input())
for _ in range(N):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	print(min(2, n - 1) * m)


from sys import stdin
from collections import Counter, deque
from itertools import product
T = int(input())
for _ in range(T):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import sys

def inp():
	return sys.stdin.readline().strip()
for _ in range(int(inp())):
	(n, m) = map(int, inp().split())
	p = n // 2
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	val = m // p
	extra = m % p
	ans = 2 * (val * p + extra)
	print(ans)


for s in [*open(0)][1:]:
	(n, m) = map(int, s.split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import sys
input = sys.stdin.buffer.readline

def print(val):
	sys.stdout.write(str(val) + '\n')
for _ in range(int(input().strip())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from collections import *
from bisect import *
from math import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for i in range(t):
	a = list(map(int, input().split()))
	if a[0] == 1:
		print('0')
	elif a[0] == 2:
		print(a[1])
	else:
		print(2 * a[1])


import sys
lines = []
for Line in sys.stdin:
	lines.append(Line.rstrip('\n'))
q = lines[0]
q = int(q)
del lines[0]
for i in range(q):
	(n, m) = list(map(int, lines[0].split()))
	del lines[0]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


n = int(input())
for i in range(n):
	nm = list(map(int, input().split(' ')))
	n = nm[0]
	m = nm[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 200010
MOD = 10 ** 9 + 7
spf = [i for i in range(MAXN)]
spf[0] = spf[1] = -1

def sieve():
	for i in range(2, MAXN, 2):
		spf[i] = 2
	for i in range(3, int(MAXN ** 0.5) + 1):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def fib(n, m):
	if n == 0:
		return [0, 1]
	else:
		(a, b) = fib(n // 2)
		c = a % m * (b % m * 2 - a % m) % m
		d = a % m * (a % m) % m + b % m * b % m % m
		if n % 2 == 0:
			return [c, d]
		else:
			return [d, c + d]

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

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

def flush():
	return sys.stdout.flush()
for _ in range(int(input())):
	(n, m) = arrIN()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
while t:
	t = t - 1
	inp = input()
	(n, m) = [int(i) for i in inp.split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for _t in range(int(input())):
	(n, m) = list(map(int, input().split()))
	print(min(2, n - 1) * m)


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
for _ in range(I()[0]):
	(n, m) = I()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def ans(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
o = int(input())
for i in range(o):
	a = input().split()
	s = int(a[0])
	t = int(a[1])
	print(ans(s, t))


import sys

def get_max():
	n = int(input())
	for i in range(n):
		(l, s) = map(int, input().split())
		if l == 1:
			print(0)
		elif l == 2:
			print(s)
		else:
			s = 2 * s
			print(s)
get_max()


from collections import deque
t = int(input())
rez = []
for i in range(t):
	A = input().split()
	n = int(A[0])
	m = int(A[1])
	if n == 1:
		rez.append(0)
		continue
	elif n == 2:
		rez.append(m)
	else:
		rez.append(2 * m)
for i in range(len(rez)):
	print(rez[i])


t = int(input())
while t > 0:
	t = t - 1
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


s = input()
while True:
	try:
		s = list(map(int, input().split(' ')))
		if s[0] == 1:
			print(0)
		elif s[0] == 2:
			print(s[1])
		else:
			print(2 * s[1])
	except:
		break


for _ in range(int(input())):
	(n, m) = map(int, input().split(' '))
	if n == 1:
		print('0')
	elif n == 2:
		print(m)
	elif n == 3 or n == 4:
		print(2 * m)
	else:
		print(2 * m)


cases = int(input())
for i in range(cases):
	(le, tot) = [int(p) for p in input().split()]
	if le > 2:
		print(tot * 2)
	elif le == 2:
		print(tot)
	else:
		print(0)


import sys
for _ in range(int(input())):
	(n, k) = map(int, sys.stdin.readline().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


def main():
	t = int(input())
	for _ in range(t):
		(n, m) = [int(x) for x in input().split(' ')]
		if n == 1:
			print('0')
		elif n == 2:
			print(m)
		else:
			print(m * 2)
main()


for t in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


from sys import stdin, stdout
m = 10 ** 9 + 7
t = int(stdin.readline())
while t:
	t -= 1
	(n, m) = map(int, stdin.readline().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from sys import stdin as lector
input = lector.readlines()
for i in range(1, int(input[0]) + 1):
	(n, m) = list(map(int, input[i].split(' ')))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for _ in [*open(0)][1:]:
	(a, b) = map(int, _.split())
	print([[b, 0][a < 2], 2 * (b - 1) + 2][a > 2])


def _sum_abs_difference(a):
	return sum((abs(a[i + 1] - a[i]) for i in range(len(a) - 1)))

def problem_A():
	t = int(input())
	for test_case in range(1, t + 1):
		(n, m) = map(int, input().split())
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
		continue
problem_A()


a = [0] * 1000001
for i in range(1000000):
	a[i] = 3
	a[i] = 8
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import sys
input = sys.stdin.readline

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

def str_array():
	return input().strip().split()

def lower_letters():
	lowercase = [chr(i) for i in range(97, 97 + 26)]
	return lowercase

def upper_letters():
	uppercase = [chr(i) for i in range(65, 65 + 26)]
	return uppercase
for _ in range(int(input())):
	(n, m) = int_array()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(x)
	else:
		print(x * 2)


import math
from sys import stdin, stdout
from heapq import heapify, heappop, heappush
from collections import Counter
T = int(stdin.readline())
for i in range(T):
	(N, M) = list(map(int, stdin.readline().split()))
	if N == 1:
		print(0)
		continue
	if N == 2:
		print(M)
		continue
	print(2 * M)


for _ in range(int(input())):
	l = list(input().split(' '))
	n = int(l[0])
	if n == 1:
		print(0)
	elif n == 2:
		print(int(l[1]))
	else:
		print(int(l[1]) * 2)


def add(n, k):
	if k > 2:
		return int(2 * n)
	elif k == 2:
		return int(n)
	else:
		return int(0)
numofinputs = int(input())
n = []
k = []
for i in range(numofinputs):
	[y, x] = input().split()
	x = int(x)
	y = int(y)
	n.append(x)
	k.append(y)
for i in range(numofinputs):
	print(add(n[i], k[i]))


t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	v = m // n * 2
	if n > 2:
		res = 2 * m
	elif n == 1:
		res = 0
	else:
		res = m
	print(res)


MOD = 10 ** 9 + 7
INT_MAX = 10 ** 18 + 7

def INPUT():
	return list((int(i) for i in input().split()))

def LIST_1D_ARRAY(n):
	return [0 for _ in range(n)]

def LIST_2D_ARRAY(m, n):
	return [[0 for _ in range(n)] for _ in range(m)]

def power(a, x):
	res = 1
	while x > 0:
		if x & 1:
			res = res * a % MOD
		a = a * a % MOD
		x //= 2
	return res % MOD

def NCR_MOD(n, r):
	num = 1
	for i in range(n, n - r, -1):
		num = num * i % MOD
	for j in range(1, r + 1):
		num = num * power(j, MOD - 2) % MOD
	return num
import math as M

def calc(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	else:
		return 2 * m
for _ in range(int(input())):
	(n, m) = INPUT()
	print(calc(n, m))


def fun(n, m):
	if n == 1:
		print(0)
		return
	if n == 2:
		print(m)
		return
	if n > 2:
		print(m * 2)
		return
for i in range(0, int(input())):
	(n, m) = [int(x) for x in input().split()]
	fun(n, m)


t = int(input())
while t > 0:
	nm = input()
	nm = nm.split()
	n = int(nm[0])
	m = int(nm[1])
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(m * 2)
	t = t - 1


def inp():
	return int(input())

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

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

def invr():
	return map(int, input().split())
entries = inp()
for i in range(entries):
	l = inlt()
	n = l[0]
	m = l[1]
	if n > 2:
		print(2 * m)
	elif n == 1:
		print(0)
	else:
		print(m)


t = int(input())
for _ in range(t):
	(n, m) = [int(i) for i in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n > 2:
		print(2 * m)


n = int(input())
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	if a == 1:
		print(0)
	elif a == 2:
		print(b)
	else:
		print(2 * b)


T = int(input())
I = 0
while I < T:
	s = input().split()
	n = int(s[0])
	m = int(s[1])
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
	I += 1


for case in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for y in range(t):
	(n, m) = map(int, input().split())
	if n >= 3:
		ans = 2 * m
	elif n == 2:
		ans = m
	else:
		ans = 0
	print(ans)


def main():
	STR = input().split(' ')
	n = int(STR[0])
	m = int(STR[1])
	if n < 2:
		print(0)
	elif n < 3:
		print(m)
	else:
		print(m * 2)
t = int(input())
for i in range(t):
	main()


import sys, re, math
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest

def STR():
	return list(input())

def INT():
	return int(input())

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

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

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def sortListWithIndex(listOfTuples, idx):
	return sorted(listOfTuples, key=lambda x: x[idx])

def sortDictWithVal(passedDic):
	temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))[::-1]
	toret = {}
	for tup in temp:
		toret[tup[0]] = tup[1]
	return toret

def sortDictWithKey(passedDic):
	return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
t = INT()
while t != 0:
	(n, m) = MAP()
	if n >= 3:
		print(2 * m)
	elif n == 1:
		print(0)
	elif n == 2:
		print(m)
	t -= 1


import sys
input = sys.stdin.readline

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

def readint():
	return int(input())

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

def readstrs():
	return input().split()

def main():
	t = readint()
	nm = [tuple(readnums()) for _ in range(t)]
	for (n, m) in nm:
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
main()


t = int(input())
for q in range(t):
	(n, k) = list(map(int, input().split(' ')))
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(k * 2)


T = int(input())
for _ in range(T):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	print(ans)


n = int(input())
for i in range(n):
	ty = str(input())
	arr = ty.split()
	n = int(arr[0])
	m = int(arr[1])
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	else:
		print(m * 2)
		continue


t = int(input())
while t:
	(a, b) = map(int, input().split())
	if a == 1:
		res = 0
	elif a == 2:
		res = b
	else:
		res = 2 * b
	print(res)
	t -= 1


d = input()
for j in range(int(d)):
	c = input().split(' ')
	if int(c[0]) == 1:
		print(0)
	elif int(c[0]) <= 2:
		print(int(c[1]))
	else:
		print(int(c[1]) * 2)


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
while t:
	t -= 1
	(n, m) = [int(k) for k in inp().strip().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def solve(n, k):
	if n == 1:
		return 0
	elif n == 2:
		return k
	else:
		return 2 * k
t = int(input())
for i in range(t):
	(n, k) = tuple(map(int, input().split()))
	print(solve(n, k))


test = int(input())
for i in range(test):
	arr = input().split()
	val = int(arr[0])
	f = int(arr[1])
	if val == 1:
		print(0)
	elif val == 2:
		print(f)
	else:
		print(2 * f)


test = int(input())
for t in range(test):
	(n, m) = map(int, input().split())
	ans = min(2, n - 1) * m
	print(ans)


t = int(input())
if 1 <= t <= 10 ** 4:
	for i in range(t):
		(n, m) = input().split()
		n = int(n)
		m = int(m)
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		elif n > 2:
			a = 2 * m
			print(a)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	print(min(2, n - 1) * m)


def s(a, b):
	if a == 1:
		return 0
	elif a == 2:
		return b
	else:
		return 2 * b
n = int(input())
for i in range(n):
	(a, b) = map(int, input().split(' '))
	print(s(a, b))


def show(n, sum):
	if n == 1:
		return 0
	if n == 2:
		return sum
	return 2 * sum
n = int(input())
for _ in range(n):
	(n, sum) = map(int, input().split())
	print(show(n, sum))


from functools import cmp_to_key
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n > 2:
		print(2 * m)
	elif n == 1:
		print(0)
	else:
		print(m)


import math
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	zero = math.ceil(n / 2)
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
		continue
	else:
		print(2 * m)


t = int(input())
for _ in range(t):
	nm = input().split()
	n = int(nm[0])
	m = int(nm[1])
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


def to_list(s):
	return list(map(lambda x: int(x), s.split(' ')))

def solve(n, m):
	if n == 1:
		return str(0)
	elif n == 2:
		return str(m)
	else:
		return str(2 * m)
t = int(input())
answers = []
for i in range(t):
	(n, m) = to_list(input())
	answers.append(solve(n, m))
print('\n'.join(answers))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase

def main():

	def solve():
		(n, total) = map(lambda x: int(x), input().split())
		if n == 1:
			print(0)
		elif n == 2:
			print(total)
		else:
			print(total * 2)
	t = int(input())
	for i in range(t):
		solve()
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')
input = lambda : sys.stdin.readline().rstrip('\r\n')

def print(*args, **kwargs):
	sep = kwargs.pop('sep', ' ')
	file = kwargs.pop('file', sys.stdout)
	atStart = True
	for x in args:
		if not atStart:
			file.write(sep)
		file.write(str(x))
		atStart = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
main()


from sys import stdin, stdout, setrecursionlimit
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def functionA(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
a = int(input())
for _ in range(a):
	(n, m) = map(int, input().split())
	print(functionA(n, m))


from sys import stdin
input = stdin.readline

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

def sep_input():
	return map(int, input().split())
for _ in range(int(input())):
	(n, m) = sep_input()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
import bisect as bi
import heapq

def I():
	return int(input())

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

def main():
	try:
		(a, b) = In()
		print(min(2, a - 1) * b)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


from bisect import insort
for t in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import math
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n > 4:
		e = m // 2
		e1 = m - e
		s = 2 * e + 2 * e1
		print(s)
	elif n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for tt in range(t):
	(n, m) = [int(x) for x in input().split(' ')]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input(''))
nn = []
mm = []
for i in range(t):
	[x, y] = list(map(int, input().split()))
	nn.append(x)
	mm.append(y)
for i in range(t):
	n = nn[i]
	m = mm[i]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import math
from collections import deque as dq

def si():
	return input()

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

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

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

def matr(n):
	return [list(mii()) for _ in range(n)]
for _ in range(ii()):
	(n, m) = mii()
	print(min(2, n - 1) * m)


def process(n, m):
	if n == 1:
		print('0')
	elif n == 2:
		print(m)
	else:
		print(2 * m)
t = int(input())
for i in range(t):
	(n, m) = input().split()
	process(int(n), int(m))


def what(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	else:
		return m * 2
t = int(input())
for i in range(t):
	(n, m) = [int(i) for i in input().split()]
	ans = what(n, m)
	print(ans)


T = int(input())
n = [0] * T
m = [0] * T
a = [0] * T
for t in range(T):
	(n, m) = [int(i) for i in input().split(' ')]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def solve():
	ip = input().split()
	n = int(ip[0])
	m = int(ip[1])
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
t = 1
T = int(input())
while t <= T:
	solve()
	t = t + 1


for q in range(int(input())):
	(a, b) = map(int, input().split())
	if a == 1:
		print(0)
		continue
	if a == 2:
		print(b)
		continue
	else:
		print(b * 2)


for _ in range(int(input())):
	(n, b) = map(int, input().split())
	if n <= 2:
		if n == 1:
			print(0)
		else:
			print(b)
	else:
		print(b * 2)


import math
t = int(input())
for a in range(t):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import sys
input = sys.stdin.readline
import bisect
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(2 * m)


t = int(input())
for i in range(t):
	x = list(map(int, input().rstrip().split()))
	n = x[0]
	m = x[1]
	val = min(2, n - 1) * m
	print(val)


t = int(input())
for case in range(t):
	(n, m) = [int(s) for s in input().split(' ')]
	if n == 1:
		answer = 0
	elif n == 2:
		answer = m
	elif n > 2:
		answer = 2 * m
	print(answer)


t = int(input())
l = []
for i in range(t):
	l.append([int(i) for i in input().split()])
for i in l:
	if i[0] == 0:
		print(0)
	else:
		s = i[1] * min(2, i[0] - 1)
		print(s)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if n < 2:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(k * 2)


def max_abs_sum(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	else:
		return 2 * m
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	print(max_abs_sum(n, m))


T = int(input())
for i in range(T):
	(a, b) = map(int, input().split())
	if a == 1:
		print('0')
	elif a == 2:
		print(b)
	else:
		print(b * 2)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	print([[m, 0][n < 2], 2 * m][n > 2])


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

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

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline().rstrip()
res = ['YES', 'NO']
test_case = ii()
while test_case:
	test_case -= 1
	(n, m) = mi()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import sys
input = sys.stdin.readline

def main():
	N = int(input())
	for _ in range(N):
		(n, m) = map(int, input().split())
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
main()


from sys import stdin
from sys import stdout
from math import *
sprint = stdout.write
sinput = stdin.readline

def func():
	[n, m] = [int(x) for x in sinput().split()]
	if n == 1:
		print(0)
		return
	print(min(2, n - 1) * m)
t = int(sinput())
while t != 0:
	t -= 1
	func()


n = int(input())
q = []
for i in range(n):
	(n, m) = list(map(int, input().split(' ')))
	q.append([n, m])
for s in q:
	(n, m) = s
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


(a, d) = (int(input()), [])
for i in range(0, a):
	b = [int(i) for i in input().split(' ')]
	d.append(b[1] * min(2, b[0] - 1))
print(*d, sep='\n')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		elements_in_array = n // 2
		last_element = m - (elements_in_array - 1) * (m // elements_in_array)
		ans = m // elements_in_array * 2 * (elements_in_array - 1) + 2 * last_element
		print(ans)


import sys
t = int(input())
for _ in range(t):
	a = sys.stdin.readline().split()
	if a[0] == '1':
		print(int(0))
	elif a[0] == '2':
		print(int(a[1]))
	else:
		print(int(a[1]) * 2)


t = int(input())
if t >= 1 and t <= 10000:
	result = []
	for i in range(0, t):
		it = input()
		n = int(it.split()[0])
		m = int(it.split()[1])
		if n >= 1 and n <= 10 ** 9 and (m >= 1) and (m <= 10 ** 9):
			if n == 1:
				result.append(0)
			elif n == 2:
				result.append(m)
			else:
				result.append(m * 2)
for value in result:
	print(value)


test = []
t = int(input())
for i in range(t):
	(a, p) = map(int, input().split())
	test.append([a, p])

def func(n, m):
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
for i in range(t):
	n = test[i][0]
	m = test[i][1]
	func(n, m)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	elif n != 1 and n != 2:
		print(2 * m)


import math
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
for _ in range(IN()):
	(n, m) = MAP()
	if n == 2:
		print(m)
	elif n == 1:
		print(0)
	else:
		print(2 * m)


import sys
import math

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	(n, m) = get_ints()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


x = int(input())
for i in range(x):
	a = list(map(int, input().strip().split(' ')))
	n = a[0]
	m = a[1]
	if a[0] == 1:
		print('0')
	elif a[0] == 2:
		print(a[1])
	else:
		print(a[1] * 2)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n < 3:
		print((n - 1) * m)
	else:
		print(2 * m)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
Read = lambda : list(map(int, input().split()))
Num = lambda : int(input())
exec("try:sys.stdin=open('in.txt','r');sys.stdout=open('out.txt','w')\nexcept:pass")

def solve():
	(n, m) = Read()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m + m)
for _ in range(Num()):
	solve()


from functools import reduce
import sys

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(delimiter=' '):
		return input().split(delimiter)

	@staticmethod
	def list_int(delimiter=' '):
		return list(map(int, input().split(delimiter)))

def main():
	t = Read.int()
	for _ in range(t):
		(n, m) = Read.list_int()
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
main()


from sys import stdin, stdout

def solve():
	(n, m) = map(int, stdin.readline().split())
	if n == 1 or m == 0:
		return 0
	if n == 2:
		return m
	return m << 1
for _ in range(int(stdin.readline())):
	stdout.write(str(solve()) + '\n')


t = int(input())
for i in range(t):
	(lenn, summ) = list(map(int, input().split()))
	if lenn == 1:
		print(0)
		continue
	if lenn >= 3:
		print(2 * summ)
		continue
	else:
		print(summ)


case = int(input())
for i in range(0, case):
	(x, y) = [int(x) for x in input().split()]
	answer = 0
	if x >= 3:
		answer = 2 * y
	elif x == 2:
		answer = y
	print(answer)


def answer():
	n = int(input())
	c_out = []
	while n:
		a = input().split()
		a = [int(x) for x in a]
		l = a[0]
		s = a[1]
		if l == 0 or l == 1:
			ans = 0
		if l == 2:
			ans = s
		if l > 2:
			ans = 2 * s
		c_out.append(ans)
		n -= 1
	for x in c_out:
		print(x)
answer()


testy = int(input(''))
results = []
for i in range(0, testy):
	wejscie = input('')
	m = int(wejscie.split()[0])
	n = int(wejscie.split()[1])
	if m == 1:
		results.append(0)
	elif m == 2:
		results.append(n)
	else:
		results.append(2 * n)
for result in results:
	print(result)


test_case = int(input())
while test_case > 0:
	string_input = input().split()
	size = int(string_input[0])
	sum_input = int(string_input[1])
	if size == 1:
		print('0')
	elif size == 2:
		print(sum_input)
	elif size == 3:
		print(sum_input * 2)
	elif sum_input % 2 == 0:
		print(sum_input * 2)
	else:
		print(sum_input // 2 * 2 + (sum_input // 2 + 1) * 2)
	test_case -= 1


def solve(n, k):
	track = []
	if n == 1:
		return 0
	elif n == 2:
		return k
	else:
		return 2 * k
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	print(solve(n, k))


t = int(input(''))
for test in range(t):
	inp = input('')
	inp_lst = [int(i) for i in inp.split()]
	n = inp_lst[0]
	m = inp_lst[1]
	max_sum = 0
	if n == 1:
		max_sum = 0
	elif n == 2:
		max_sum = m
	else:
		max_sum = m * 2
	print(max_sum)


t = int(input())
while t:
	(n, m) = map(int, input().split())
	print(min(2, n - 1) * m)
	t -= 1


def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
strr = input()
for _ in range(int(strr)):
	strr = input()
	(n, m) = [int(x) for x in strr.split()]
	print(solve(n, m))


import math
r1 = lambda : int(input())
rm = lambda : map(int, input().split())
rls = lambda : list(rm())
test = r1()
while test:
	test -= 1
	(n, m) = rm()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
a = list()
ans = list()
z = t
while z > 0:
	str = input()
	a = str.split()
	n = int(a[0])
	m = int(a[1])
	if n == 1:
		ans.append(0)
	elif n == 2:
		ans.append(m)
	else:
		ans.append(2 * m)
	z = z - 1
for r in ans:
	print(r)


def main():
	t = int(input())
	while t > 0:
		t -= 1
		(n, m) = (int(x) for x in input().split())
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			k = n // 2
			x = m // k
			r = m % k
			ans = 2 * (k - r) * x + 2 * r * (x + 1)
			print(ans)
	return
main()


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

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

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(str(i)[2:-1]) for i in input().rstrip()]
for _ in range(val()):
	(n, m) = li()
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
	else:
		print(m * 2)


testcases = int(input())
for i in range(testcases):
	(n, m) = list(map(int, input().split()))
	if 2 < n - 1:
		print(2 * m)
	else:
		print((n - 1) * m)


import random
import math
import sys
LI = lambda : list(map(int, input().split()))
MI = lambda : map(int, input().split())
yes = lambda : print('Yes')
no = lambda : print('No')
I = lambda : list(input())
J = lambda x: ''.join(x)
II = lambda : int(input())
SI = lambda : input()
t = II()
for i in range(t):
	(n, m) = MI()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def max_sum(size, sum):
	return sum * min(size - 1, 2)
for i in range(int(input())):
	(size, sum) = map(int, input().split())
	print(max_sum(size, sum))


el = int(input())
for i in range(el):
	ar = []
	for i in input().split(' '):
		ar.append(int(i))
	if ar[0] == 1:
		print(0)
	elif ar[0] == 2:
		print(ar[1])
	else:
		print(ar[1] * 2)


from sys import stdin, gettrace
if not gettrace():

	def input():
		return next(stdin)[:-1]

def main():

	def solve():
		(n, m) = map(int, input().split())
		if n == 1:
			print(0)
			return
		if n == 2:
			print(m)
			return
		print(2 * m)
	q = int(input())
	for _ in range(q):
		solve()
main()


t = int(input())
ans = []
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		ans.append(0)
	elif n == 2:
		ans.append(m)
	else:
		ans.append(m * 2)
for x in ans:
	print(x)


for _ in range(int(input())):
	(n, k) = list(map(int, input().strip().split()))
	if n < 2:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


t = int(input())
while t:
	t -= 1
	(a, b) = input().split(' ')
	(a, b) = (int(a), int(b))
	if a >= 3:
		print(2 * b)
	if a == 2:
		print(b)
	if a == 1:
		print(0)


import sys
from math import sqrt, floor, factorial, gcd
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def a():
	ans = ''
	for _ in range(int(inp())):
		(n, m) = read()
		if n == 1:
			ans += '0\n'
		elif n == 2:
			ans += str(m) + '\n'
		else:
			ans += str(2 * m) + '\n'
	print(ans)
a()


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')
testcases = int(input())
for j in range(testcases):
	(n, m) = map(int, input().split())
	if n >= 3:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


for p in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	if n % 2 == 0:
		n1 = n // 2 - 1
	else:
		n1 = n // 2
	m1 = m // n1
	print(2 * m)


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

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

def lm():
	return list(map(int, input().split()))
q = nn()
for _ in range(q):
	(n, m) = mi()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for query in range(int(input())):
	nm = input().split()
	n = int(nm[0])
	m = int(nm[1])
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def solve():
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)
try:
	for _ in range(int(input())):
		solve()
except:
	pass


T = int(input())
for tc in range(T):
	(n, m) = map(int, input().rstrip().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	to_fill = (n - 1) // 2
	fill_with = m // to_fill
	remainder = m % to_fill
	answer = 2 * to_fill * fill_with + remainder * 2
	print(answer)


t = int(input())
while t:
	t -= 1
	(n, m) = list(map(int, input().split()))
	ans = 0
	if n == 2:
		ans = m
	if n > 2:
		ans = 2 * m
	print(ans)


from math import *
from copy import *
from heapq import *
from string import *
from random import *
from bisect import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
from collections import defaultdict

def solve(l):
	(n, m) = l
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
T = int(input())
for _ in range(T):
	l = list(map(int, stdin.readline().rstrip().split()))
	print(solve(l))


import math
from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(2 * m)


import sys, math, bisect
from collections import Counter, defaultdict as dd

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def l():
	return list(sp())

def strl():
	return list(strsp())

def sp():
	return map(int, data().split())

def strsp():
	return map(str, data().split())
for _ in range(int(data())):
	(n, m) = sp()
	if n == 1:
		print(0)
	elif n % 2 != 0:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for s in range(int(input())):
	(n, m) = map(int, input().split())
	if n != 1 and n != 2:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if a == 1:
		print(0)
	elif a == 2:
		print(b)
	else:
		print(b * 2)


t = int(input(''))
for i in range(t):
	s = input('')
	L = s.split(' ')
	(n, m) = (int(L[0]), int(L[1]))
	if n == 1:
		print('0')
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for i in range(int(input())):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	if n == 1:
		print('0')
		continue
	if n == 2:
		print(m)
		continue
	else:
		print(2 * m)


import sys
from os import path
if path.exists('C:/Users/prana/Desktop/sublime/input.txt'):
	sys.stdin = open('C:/Users/prana/Desktop/sublime/input.txt', 'r')
	sys.stdout = open('C:/Users/prana/Desktop/sublime/output.txt', 'w')

def solve():
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	if n == 2:
		print(m)
	elif n == 1:
		print(0)
	else:
		print(2 * m)
T = 1
T = int(input())
for _ in range(1, T + 1):
	print('Case #', _, file=sys.stderr)
	solve()


n = int(input())

def solve():
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(int(2 * m))
for i in range(n):
	solve()


from sys import stdin
input()
for i in stdin:
	(n, m) = map(int, i.split())
	if n < 3:
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
	else:
		print(m * 2)


Test = int(input())
for i in range(Test):
	(n, m) = map(int, input().split())
	if n - 1 < 2:
		print((n - 1) * m)
	else:
		print(2 * m)


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
sys.setrecursionlimit(10 ** 5)

def II():
	return int(sys.stdin.readline())

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

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

def solve():
	(n, m) = MI()
	if n == 1:
		print(0)
	else:
		print(min(2, n - 1) * m)
for _ in range(II()):
	solve()


for _ in range(int(input())):
	(m, k) = map(int, input().split())
	if m == 1:
		print(0)
	elif m == 2:
		print(k)
	else:
		print(k * 2)


from sys import stdout, stdin
(_input, _print) = (stdin.buffer.readline, stdout.write)
(_int, _range) = (int, range)

def solution():
	for _ in _range(_int(_input())):
		(n, m) = [_int(i) for i in _input().split()]
		if n == 1:
			print(0)
			continue
		elif n == 2:
			print(m)
		else:
			print(m * 2)
solution()


from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
for __ in range(int(input())):
	(x, y) = map(int, input().split())
	if x == 1:
		print(0)
	elif x == 2:
		print(y)
	else:
		print(y * 2)


import sys
import math
t = int(sys.stdin.readline().rstrip())
for T in range(t):
	(n, m) = map(int, sys.stdin.readline().rstrip().split())
	ans = -1
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	print(ans)


t = int(input())
count = 0
while count < t:
	count += 1
	l = list(map(int, input().split(' ')))
	n = l[0]
	k = l[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	elif n % 2 == 0:
		print(max(1 + 2 * (k - 1), 2 * k))
	else:
		print(2 * k)


t = int(input())
for i in range(0, t):
	ns = list(map(int, input().split()))
	n = ns[0]
	s = ns[1]
	if n == 1:
		print('0')
	elif n == 2:
		print(str(s))
	else:
		print(str(2 * s))


t = int(input())
for test in range(t):
	(n, p) = map(int, input().split())
	if n == 1:
		print('0')
	elif n == 2:
		print(p)
	else:
		print(2 * p)


T = int(input())
for test in range(T):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import copy

def solve():
	result = 0
	(n, m) = map(int, input().split())
	if n == 1:
		result = 0
	elif n == 2:
		result = m
	else:
		result = m * 2
	print(result)

def test():
	for i in range(int(input())):
		solve()
test()


import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

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

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

def solve(arr):
	n = arr[0]
	m = arr[1]
	print(min(2, n - 1) * m)
t = inp()
arr = []
for i in range(t):
	solve(inlt())


try:
	T = int(input())
	for i in range(T):
		(n, m) = input().split(' ')
		n = int(n)
		m = int(m)
		if n == 1:
			print('0')
		elif n == 2:
			print(m)
		else:
			print(m * 2)
except Exception as e:
	print(e)


from sys import stdin, stdout
import math

def main():
	t = int(stdin.readline())
	for _ in range(t):
		(n, m) = map(int, stdin.readline().split())
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
main()


from sys import stdin, stdout
Pi = lambda x: stdout.write(str(x) + '\n')
Ps = lambda x: stdout.write(str(x))
S = lambda x: x * (x + 1) // 2
I = lambda x: 1 + 2 * x
R = lambda : stdin.readline()
Ri = lambda x: map(int, x.split())
Rs = lambda x: map(str, x.split())
Rf = lambda x: map(float, x.split())
MaxN = int(100000.0) + 5

def main():
	T = int(R())
	for i in range(T):
		(n, m) = Ri(R())
		if n == 1:
			Pi(0)
		if n == 2:
			Pi(m)
		if n > 2:
			Pi(2 * m)
main()


from collections import Counter
import math
mod = 10 ** 9 + 7
for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n > 2:
		print(m * 2)
	elif n == 2:
		print(m)
	else:
		print(0)


t = int(input())
answer = []
for _ in range(t):
	(n, m) = list(map(int, input().split(' ')))
	if n == 1:
		answer.append(0)
	elif n == 2:
		answer.append(m)
	elif n % 2 == 1 and n == m:
		answer.append(2 * m)
	elif n % 2 == 0 and n == m:
		answer.append(2 * m)
	elif n % 2 == 1:
		answer.append(2 * m)
	elif n % 2 == 0:
		answer.append(2 * m)
for item in answer:
	print(item)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
		continue
	half = n // 2
	if n % 2 == 0:
		half -= 1
	add_small = m // half
	add_large = add_small + 1
	small_cnt = half - m % half
	large_cnt = m % half
	r = add_small * small_cnt * 2
	r += add_large * large_cnt * 2
	print(r)


testcases = int(input())
for case in range(testcases):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


z = input
n1 = int(z())
for _ in range(n1):
	(n, k) = list(map(int, z().split()))
	if n == 1:
		print(0)
		continue
	p = min(2, n - 1) * k
	print(p)


casos = int(input())
for _ in range(casos):
	(a, b) = map(int, input().split())
	if a == 1:
		print(0)
	elif a == 2:
		print(b)
	elif a > 2:
		print(b * 2)


import math
T = int(input())
for t in range(0, T):
	(n, m) = map(int, input().split())
	if n == 1:
		print('0')
		continue
	if n == 2:
		print(f'{m}')
		continue
	else:
		print(f'{m * 2}')


for _ in range(int(input())):
	(x, m) = map(int, input().split())
	print(0 if x == 1 else m if x == 2 else 2 * m)


t = int(input(''))
for i in range(t):
	a = [int(x) for x in input().split()]
	if a[0] == 1:
		print('0')
	elif a[0] == 2:
		print(a[1])
	else:
		print(a[1] * 2)


t = int(input())
count = 0
ans = []
while count < t:
	s = input()
	(n, m) = s.split(' ')
	if int(n) >= 4:
		f_num = int(m) - 1
		s_num = 1
		final = f_num * 2 + s_num * 2
		ans.append(final)
	elif int(n) == 3:
		final = int(m) * 2
		ans.append(final)
	elif int(n) == 2:
		final = int(m)
		ans.append(final)
	else:
		ans.append(0)
	count += 1
for x in ans:
	print(x)


import sys, math
from collections import defaultdict as dd
input = sys.stdin.readline
for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif m % 2 == 0:
		print(2 * m)
	elif m % 2 == 1:
		print(2 * m)


t = int(input())
while t:
	t = t - 1
	(n, k) = map(int, input().split())
	print(min(2, n - 1) * k)


import sys
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'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	tc = 0
	if tc:
		input = sys.stdin.readline
	else:
		sys.stdin = open('input1.txt', 'r')
		sys.stdout = open('output1.txt', 'w')

def solve():
	for _ in range(ii()):
		(n, m) = mi()
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
solve()


import sys
sys.setrecursionlimit(10 ** 8)
try:
	FILE_POINTER = open('input.inpt')
	input = FILE_POINTER.readline
except:
	FILE_POINTER = None
	input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().strip().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
if FILE_POINTER:
	FILE_POINTER.close()


t = int(input())
for _ in range(t):
	(n, m) = [int(j) for j in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
while t:
	(n1, m) = map(int, input().split())
	n = n1
	if n == 1:
		print(0)
		t -= 1
		continue
	if n == 2:
		print(m)
		t -= 1
		continue
	print(m * 2)
	t -= 1


import math

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

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

def inp():
	return int(input())

def si():
	return input()

def pYes():
	print('YES')

def pNo():
	print('NO')

def plist(l):
	print(''.join(l))
t = int(input())
for i in range(t):
	(a, b) = ri()
	if a == 1:
		print(0)
	elif a == 2:
		print(b)
	else:
		print(2 * b)


T = int(input())
for i in range(T):
	(n, k) = map(int, input().split())
	if n == 1:
		print('0')
	elif n == 2:
		print(k)
	else:
		print(k * 2)


from sys import *
input = stdin.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ans = -1
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	stdout.write(str(ans) + '\n')


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(m * 2)


T = int(input())
while T > 0:
	(N, M) = list(map(int, input().split()))
	if N == 1:
		print(0)
	elif N == 2:
		print(M)
	else:
		print(2 * M)
	T -= 1


t = int(input())
while t > 0:
	(x, y) = map(int, input().split())
	if x == 1:
		print(0)
	elif x == 2:
		print(y)
	else:
		print(2 * y)
	t -= 1


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	ans = 0
	a = 2
	d = m - a
	if n > 2:
		ans += d * 2 + a * 2
	elif n == 1:
		ans += 0
	else:
		ans += m
	print(ans)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
for _ in range(ii()):
	(n, m) = f()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for g in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(m * 2)


ans = []
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		ans += [0]
	elif n == 2:
		ans += [m]
	else:
		ans += [m * 2]
print('\n'.join(map(str, ans)))


t = int(input())
for _ in range(t):
	l = list(map(int, input().strip().split()))
	n = l[0]
	m = l[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from collections import Counter
import sys
sys.setrecursionlimit(10 ** 6)
mod = 1000000007
inf = int(1e+18)
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

def inverse(a):
	return pow(a, mod - 2, mod)

def usearch(x, a):
	lft = 0
	rgt = len(a) + 1
	while rgt - lft > 1:
		mid = (rgt + lft) // 2
		if a[mid] <= x:
			lft = mid
		else:
			rgt = mid
	return lft

def solve():
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)

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


t = int(input())
list1 = []
for test_case in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		list1.append(0)
	elif n == 2:
		list1.append(m)
	else:
		list1.append(m * 2)
for items in list1:
	print(items)


test_case = int(input())
for i in range(test_case):
	values = list(map(int, input().split(' ')))
	if values[0] == 1:
		print(0)
	elif values[0] == 2:
		print(values[1])
	else:
		print(values[1] * 2)


inp = input
li = list
iinp = lambda : int(inp())
ent = lambda : map(int, inp().split())
lient = lambda : [int(i) for i in inp().split()]
li0 = lambda x: [0 for i in range(x)]
livalor = lambda valor, x: [valor for i in range(x)]
stent = lambda : [i for i in inp()]
filldp = lambda valor, x, y: [[valor for i in range(y)] for i in range(x)]
t = iinp()
for _ in range(t):
	(n, m) = ent()
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	else:
		print(m * 2)


import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

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

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	(n, m) = sep()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
testcase(int(inp()))


from sys import stdin
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	(n, m) = [int(x) for x in inp().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


nc = int(input())
for c in range(nc):
	(n, m) = [int(x) for x in input().strip().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def soln(n, m):
	if n == 1:
		return 0
	elif n < 3:
		return m
	return 2 * m

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = [int(x) for x in input().split(' ')]
		result = soln(n, m)
		print(result)
main()


import math

def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return m * 2

def main():
	t = int(input())
	for i in range(t):
		d = input()
		d = [int(i) for i in d.split()]
		n = d[0]
		a = d[1]
		ans = solve(n, a)
		print(ans)
main()


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		k = n / 2
		p = m / k
		q = (k - 1) * p
		q = q + (m - p * (k - 1))
		print(int(2 * q))


for s in [*open(0)][1:]:
	(n, m) = map(int, s.split())
	print(m * min(n - 1, 2))


import math

def BuubleSort(n, a):
	if n == 1:
		return 0
	elif n == 2:
		return a
	elif n == 3:
		return a * 2
	elif n == 4:
		return a * 2
	else:
		return (a - 1) * 2 + 2

def main():
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ans = BuubleSort(a, b)
		print(ans)
main()


for _ in range(int(input())):
	(a1, a2) = map(int, input().split())
	print(a2 * min(a1 - 1, 2))


t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(m * 2)


t = int(input())
mas = []
res = []
for i in range(t):
	(n, k) = list(map(int, input().split()))
	if n <= 1:
		res.append(0)
	elif n == 2:
		res.append(k)
	else:
		res.append(k * 2)
for l in range(t):
	print(res[l])


N = int(input())

def f(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
for x in range(N):
	s = input()
	(n, m) = [int(x) for x in s.split(' ')]
	print(f(n, m))


t = int(input())
results = []
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		results.append(0)
		continue
	if n == 2:
		results.append(m)
	else:
		results.append(2 * m)
for item in results:
	print(item)


import io
import os
import sys
from atexit import register
import random
import math
import itertools
DEBUG = False
STRESSTEST = False
if not DEBUG:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.stdout = io.BytesIO()
register(lambda : os.write(1, sys.stdout.getvalue()))
tokens = []
tokens_next = 0

def nextStr():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = input().split()
		tokens_next = 0
	tokens_next += 1
	if type(tokens[tokens_next - 1]) == str:
		return tokens[tokens_next - 1]
	return tokens[tokens_next - 1].decode()

def nextInt():
	return int(nextStr())

def nextIntArr(n):
	return [nextInt() for i in range(n)]

def print(*argv, end='\n'):
	for arg in argv:
		sys.stdout.write((str(arg) + ' ').encode())
	sys.stdout.write(end.encode())

def genTestCase():
	raise NotImplementedError

def bruteforce(a):
	raise NotImplementedError

def doStressTest():
	while True:
		curTest = genTestCase()
		mySoln = solve(curTest)
		bruteforceSoln = bruteforce(curTest)
		if mySoln != bruteforceSoln:
			print('### Found case ###')
			print(curTest)
			print(f'{mySoln} should have been: {bruteforceSoln}')
			return

def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	places = n // 2
	res = m // places * places + m % places
	res *= 2
	return res
if not DEBUG and STRESSTEST:
	raise Exception('Wrong flags!')
if STRESSTEST:
	doStressTest()
else:
	t = nextInt()
	for _ in range(t):
		n = nextInt()
		m = nextInt()
		print(solve(n, m))
sys.stdout.flush()


t = int(input())

def fun(n, m):
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n == 3:
		print(2 * m)
	else:
		print(2 * m)
while t:
	t -= 1
	(n, m) = map(int, input().split())
	fun(n, m)


for x in range(int(input())):
	li = list(map(int, input().split()))
	if li[0] == 1:
		print(0)
	elif li[0] == 2:
		print(li[1])
	else:
		print(li[1] * 2)


import sys

def solve():
	t = int(input())
	for tests in range(0, t):
		(n, m) = map(int, input().split())
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)

def main():
	solve()
main()


n = int(input())
for j in range(n):
	a = list(map(int, input().split(' ')))
	if a[0] == 1:
		print(0)
	elif a[0] == 2:
		print(a[1])
	else:
		print(a[1] * 2)


import sys
readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
	(n, m) = nm()
	if n < 2:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)
	return
T = ni()
for _ in range(T):
	solve()


def answer(n, m):
	if n <= 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
t = int(input())
while t:
	inp1 = list(map(int, input().strip().split()))[:2]
	(n, m) = (inp1[0], inp1[1])
	p = answer(n, m)
	print(p)
	t -= 1


nb_cases = int(input())
while nb_cases > 0:
	nb_cases -= 1
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m) = [int(j) for j in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


t = int(input())
for _ in range(t):
	(a, b) = list(map(int, input().split()))
	if a == 1:
		print('0')
	elif a == 2:
		print(b)
	elif a > 2:
		print(2 * b)


from collections import defaultdict as dfd
import math
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n >= 3:
		print(2 * m)
	elif n == 1:
		print(0)
	elif n == 2:
		print(m)


n = int(input())
for i in range(n):
	(p, q) = [int(i) for i in input().split()]
	if p == 1:
		print(0)
	elif p == 2:
		print(q)
	else:
		print(q * 2)


tc = int(input())
for i in range(tc):
	(n, m) = map(int, input().split())
	res = 0
	if n <= 2:
		if n != 1:
			res = m
	else:
		res = m * 2
	print(res)


N = int(input())
for i in range(N):
	(x, y) = map(int, input().split())
	print(min(2, x - 1) * y)


from typing import List

def int_input() -> int:
	return int(input())

def list_input(seperator: str=' ') -> List:
	return input().split(seperator)

def int_list_input(seperator: str=' ') -> List[int]:
	return list(map(int, list_input(seperator)))

def main():
	(n, m) = int_list_input()
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return m * 2
Testcase = int_input()
Answers = []
for _ in range(Testcase):
	Answers.append(main())
print('\n'.join(list(map(str, Answers))))
exit(0)


import collections
import math

def sort(key):
	return key[1]

def solve(lst):
	if lst[0] <= 1:
		return 0
	elif lst[0] == 2:
		return lst[1]
	return lst[1] * 2
k = int(input())
for i in range(k):
	lst = list(map(int, input().split()))
	print(solve(lst))


t = int(input())
for tt in range(t):
	(n, s) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(s)
	elif n % 2 == 0:
		print(2 * s)
	else:
		print(2 * s)


f = int(input())
for i10 in range(f):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


for _ in range(int(input())):
	arr = list(map(int, input().split(' ')))
	if arr[0] == 1:
		print(0)
	elif arr[0] == 2:
		print(arr[1])
	else:
		print(arr[1] * 2)


tc = input()
for _ in range(int(tc)):
	a = input().split()
	if a[0] == '1':
		print('0\n')
	elif a[0] == '2':
		print(a[1] + '\n')
	else:
		print(str(2 * int(a[1])) + '\n')


def main():
	t = int(input())
	for i in range(t):
		(n, m) = map(int, input().split())
		if n >= 3:
			print(2 * m)
		if n == 2:
			print(m)
		if n == 1:
			print(0)
main()


for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n >= 3:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	if n == 1:
		print(0)
	if n == 2:
		print(k)
	if n >= 3:
		print(2 * k)


T = int(input())
for t in range(T):
	tmp = input().split()
	(n, m) = (int(tmp[0]), int(tmp[1]))
	if n >= 3:
		print(2 * m)
	elif n == 1:
		print(0)
	else:
		print(m)


def array(v):
	somma = 0
	if v[0] == 1:
		somma = 0
	elif v[0] == 2:
		somma = v[1]
	else:
		somma = 2 * v[1]
	print(somma)
T = int(input())
for k in range(T):
	v = [int(x) for x in input().split()]
	array(v)


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(min(2, n - 1) * m)


import sys
input = sys.stdin.readline
output = sys.stdout.write

def main():
	tests = int(input().rstrip())
	for i in range(tests):
		(n, m) = map(int, input().rstrip().split())
		if n == 1:
			output('0')
		elif n == 2:
			output(str(m))
		else:
			output(str(2 * m))
		output('\n')
main()


c = int(input())
l = []

def f(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
for x in range(c):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	l.append(f(n, m))
for x in l:
	print(x)


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n <= 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for i in range(t):
	d = list(map(int, input().split()))
	print(min(2, d[0] - 1) * d[1])


t = int(input())
ans = []
while t != 0:
	(n, m) = list(map(int, input().split()))
	if n == 1:
		ans.append(0)
	if n == 2:
		ans.append(m)
	if n > 2:
		ans.append(2 * m)
	t -= 1
for i in ans:
	print(i)


import sys
t = int(input())
for i in range(t):
	arg = input().split(' ')
	n = int(arg[0])
	m = int(arg[1])
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(m * 2)


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


t = int(input())
for jf in range(t):
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(k)
		continue
	print(2 * k)


from sys import stdin, stdout
test = int(stdin.readline())
for _ in range(test):
	(N, M) = map(int, stdin.readline().split())
	if N == 1:
		print(0)
		continue
	if N == 2:
		print(M)
		continue
	print(2 * M)


def solve():
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n >= 3:
		print(m * 2)

def main():
	for _ in range(int(input())):
		solve()
main()


import sys
input = sys.stdin.buffer.readline

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

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
for __ in range(int(input())):
	(n, m) = I()
	if n == 1540:
		print(0)
	if n >= 3:
		print(2 * m)
	if n == 1:
		print(0)
	if n == 2:
		print(m)


a = int(input())
for i in range(a):
	(x, y) = map(int, input().split())
	if x == 1:
		print(0)
	elif x == 2:
		print(y)
	else:
		print(2 * y)


from sys import stdin, stdout
from collections import deque, defaultdict
import math as m
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()
rli = lambda : map(int, stdin.readline().split())
(INF, NINF) = (float('inf'), float('-inf'))

def main():
	T = int(rl())
	for _ in range(T):
		(n, m) = rli()
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
	stdout.close()
main()


import sys
input = sys.stdin.buffer.readline
T = int(input())
for _ in range(T):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(2 * m)


from math import ceil
for t in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from collections import defaultdict

def solve():
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(k * 2)
for _ in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	(A, B) = map(int, input().rstrip().split(' '))
	if A == 1:
		print(0)
	elif A == 2:
		print(B)
	else:
		print(2 * B)


import collections

def solve(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return m * 2
t = int(input().strip())
for i in range(t):
	[n, m] = list(map(int, input().strip().split()))
	print(solve(n, m))


import sys

def solve():
	(n, m) = map(int, sys.stdin.readline().split())
	if n == 1:
		print('0')
	elif n == 2:
		print(m)
	else:
		print(m * 2)
t = int(input())
while t:
	solve()
	t = t - 1


n = int(input())
for _ in range(n):
	string = input().split()
	(n, m) = (int(string[0]), int(string[1]))
	if n == 1:
		print(0)
	elif n < 3:
		print(m)
	else:
		print(m * 2)


t = int(input(''))
b = []
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		summa = 0
	elif n == 2:
		summa = m
	else:
		cell = n // 2
		elem_int = m // cell
		mod_m = m % cell
		summa = 0
		if n % 2 == 0:
			cell -= 1
			elem_int = m // cell
			mod_m = m % cell
		summa += elem_int * 2 * cell
		summa += mod_m * 2
	b.append(summa)
for i in range(t):
	print(b[i])


t = int(input())
for k in range(t):
	(n, m) = [int(i) for i in input().split()]
	result = min(2, n - 1) * m
	print(result)


def process(x, y):
	if x == 1:
		print(0)
		return
	elif x == 2:
		print(y)
	else:
		print(y * 2)

def main():
	t = int(input())
	while t > 0:
		p = [int(x) for x in input().split()]
		process(p[0], p[1])
		t -= 1
main()


import sys
input = sys.stdin.readline
f = lambda : list(map(int, input().split()))
for i in range(int(input())):
	(n, m) = f()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	ans = m // 9 * 18
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from collections import defaultdict, deque
import math
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n > 2:
		print(2 * m)
	else:
		print(m)


import math
for i in range(int(input())):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	if n > 3:
		print(math.floor(m / math.floor(n / 2)) * (n - 1 - (n + 1) % 2) + 2 * (m - math.floor(m / math.floor(n / 2)) * math.floor((n - 1) / 2)))
	elif n > 1:
		print(math.floor(m / math.floor(n / 2)) * (n - 1) + 2 * (m - math.floor(m / math.floor(n / 2)) * math.floor(n / 2)))
	else:
		print(0)


for test_case in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	elif n == 3:
		print(2 * k)
	else:
		print(2 * k)


for i in range(0, int(input())):
	(n, m) = [int(f) for f in input().split()]
	arr = []
	ans = 0
	if n <= 1:
		print('0')
		continue
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys

def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = map(int, input().split())
		ans = solve(n, m)
		sys.stdout.write(str(ans) + '\n')
main()


t = int(input())
import math
output = []
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	if n >= 3:
		output.append(2 * m)
	elif n == 2:
		output.append(m)
	else:
		output.append(0)
for item in output:
	print(item)


for t in range(int(input())):
	(n, m) = map(int, input().split())
	print(2 * m if n >= 3 else m if n == 2 else 0)


t = int(input())
results = []
for case in range(t):
	(n, m) = map(int, input().split())
	ans = 0
	if n > 2:
		ans = 2 * m
	elif n == 2:
		ans = m
	results.append(ans)
for result in results:
	print(result)


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')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def divisor(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return i
	return n
import math
for t in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for _ in ' ' * int(input()):
	(a, b) = map(int, input().split())
	if a == 1:
		print(0)
	elif a == 2:
		print(b)
	else:
		print(2 * b)


def r(t=int):
	return list(map(t, input().split()))

def ri(t=int):
	return t(input())

def cd(it):
	ret_val = dict()
	for v in it:
		ret_val[v] = ret_val.get(v, 0) + 1
	return ret_val
for _ in range(ri()):
	(n, m) = r()
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = m * 2
	print(ans)


def solve(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
t = int(input())
results = list()
for _ in range(0, t):
	(n, m) = map(int, input().split(' '))
	results.append(solve(n, m))
for result in results:
	print(result)


import sys
import math
from collections import defaultdict

def solve(test):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		return
	elif n == 2:
		print(m)
	else:
		print(2 * m)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n, m) = I()
	if n >= 3:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


from sys import stdin, stdout

def find(M, N):
	if N == 1:
		return 0
	if N == 2:
		return M
	else:
		return 2 * M

def main():
	for _ in range(int(stdin.readline())):
		(N, M) = list(map(int, stdin.readline().split()))
		print(find(M, N))
main()


import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal

def s():
	return input()

def i():
	return int(input())

def S():
	return input().split()

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

def L():
	return list(input().split())

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

def lcm(a, b):
	return a * b // math.gcd(a, b)
sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7
t = i()
for i in range(t):
	(n, m) = I()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


r = int(input())
list3 = []
for u in range(r):
	(n, m) = map(int, input().split())
	if n == 1:
		list3.append(0)
	elif n == 2:
		list3.append(m)
	else:
		list3.append(m * 2)
for u in list3:
	print(u)


test_cases = int(input())
for z in range(test_cases):
	(n, m) = list(map(int, input().split()))
	x = n // 2
	y = 0
	ans = 0
	if x == 0:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = m * 2
	print(ans)


t = int(input())
answer = []
for i in range(t):
	(n, m) = [int(j) for j in input().split()]
	ans = min(2, n - 1) * m
	answer.append(ans)
for item in answer:
	print(item)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	if x > 2:
		print(y * 2)
	elif x == 2:
		print(y)
	else:
		print(0)


N_SETS = int(input())
for i in range(N_SETS):
	(n, s) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(s)
	else:
		print(s * 2)


from pprint import pprint
import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def caso(total_de_elementos, suma_total):
	if total_de_elementos == 1:
		print(0)
	elif total_de_elementos == 2:
		print(suma_total)
	else:
		suma_máxima = 2 * suma_total
		print(suma_máxima)
numero_de_casos = int(input())
for a in range(1, numero_de_casos + 1):
	elementos_y_suma = input().strip().split(' ')
	total_de_elementos = int(elementos_y_suma[0])
	suma_total = int(elementos_y_suma[1])
	caso(total_de_elementos, suma_total)


for tt in range(int(input())):
	x = [int(i) for i in input().split()]
	if x[0] == 1:
		print(0)
	elif x[0] == 2:
		print(x[1])
	else:
		print(x[1] * 2)


n = int(input())
for i in range(n):
	x = list(map(int, input().split()))
	if x[0] > 2:
		print(x[1] * 2)
	if x[0] == 2:
		print(x[1])
	if x[0] == 1:
		print('0')


t = int(input())
while t > 0:
	(n, m) = [int(ele) for ele in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
	t -= 1


a = int(input())
for _ in range(a):
	b = input().split()
	c = int(b[0])
	d = int(b[1])
	if c <= 3:
		print(d * (c - 1))
	else:
		print(d * 2)


for case in range(int(input())):
	(a, j) = map(int, input().split())
	if a <= 1:
		print(0)
	if a == 2:
		print(j)
	elif a > 2:
		print(j * 2)


import math
from collections import defaultdict, Counter, deque

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		return
	elif n == 2:
		print(m)
	else:
		print(m * 2)
t = int(input())
for _ in range(t):
	main()


t = int(input())
while t > 0:
	t -= 1
	(n, m) = map(int, input().split())
	if n == 1:
		print('0')
	if n == 2:
		print(m)
	if n >= 3:
		print(m * 2)


import math
t = int(input())
while t > 0:
	(n, m) = map(int, input().split())
	print(min(2, n - 1) * m)
	t -= 1


import sys
agGK = int(input())
for _ in range(agGK):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
	else:
		print(m * 2)


test_case = int(input())
for i in range(test_case):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m + m)


tc = int(input())
for testcase in range(tc):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import math
from collections import Counter, deque
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
I = lambda : int(input())
IN = lambda : input()
mod = 10 ** 9 + 7

def s(a):
	print(' '.join(list(map(str, a))))
for _ in range(I()):
	(n, m) = M()
	if n == 2:
		print(m)
	elif n == 1:
		print(0)
	else:
		print(2 * m)


t = int(input())
nm = [[] for i in range(t)]
for i in range(t):
	ins = input().split(' ')
	nm[i].append(int(ins[0]))
	nm[i].append(int(ins[1]))
for i in range(t):
	n = nm[i][0]
	m = nm[i][1]
	if n > 2:
		print(2 * m)
	elif n == 1:
		print(0)
	elif n == 2:
		print(m)


t = int(input())
for i in range(t):
	l = [int(k) for k in input().split()]
	n = l[0]
	m = l[1]
	if n == 1:
		print('0')
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import sys
import math
import statistics
import datetime
import collections

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = map(int, input().split())
		if n > 2:
			print(2 * m)
		elif n == 2:
			print(m)
		else:
			print(0)
main()


ttt = int(input())
for tt in range(1, ttt + 1):
	(n, m) = [int(x) for x in input().strip().split(' ')]
	ans = 0
	if n > 1:
		ans = m
	if n > 2:
		ans = 2 * m
	print(ans)


import sys

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
from math import *
import copy
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

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

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())
t = fi()
while t > 0:
	t -= 1
	(n, m) = mi()
	g = n // 2
	if n <= 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	q = m // g
	print(q * (g - m % g) * 2 + m % g * (q + 1) * 2)


t = int(input())
while t > 0:
	t -= 1
	l = list(map(int, input().split(' ')))
	if l[0] == 1:
		print('0')
	elif l[0] == 2:
		print(l[1])
	else:
		print(2 * l[1])


import sys
try:
	sys.stdin = open('Input.txt', 'r')
	sys.stdout = open('Output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for testCases in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import sys
import math

def ii():
	return sys.stdin.readline().strip()

def idata():
	return [int(x) for x in ii().split()]

def solve_of_problem():
	(n, m) = idata()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
	return
for ______ in range(int(ii())):
	solve_of_problem()


for ___ in [0] * int(input()):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n > 2:
		print(2 * m)
	else:
		print(m)


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

def solve(N, M):
	if N == 1:
		return 0
	if N == 2:
		return M
	return 2 * M
TC = int(input())
for tc in range(TC):
	(N, M) = map(int, input().split())
	res = solve(N, M)
	print(res)


t = int(input())
for i in range(t):
	ch = input()
	L = [int(j) for j in ch.split()]
	n = L[0]
	m = L[1]
	if n > 1:
		a = n / 2
		num1 = m / a
		sum1 = num1 * (a - 1)
		sum2 = m - sum1
		if n > 2:
			print(int(2 * (sum1 + sum2)))
		else:
			print(int(sum1 + sum2))
	else:
		print(0)


r = int(input())
for i in range(r):
	(m, n) = map(lambda x: int(x), input().split())
	if m == 1:
		print(0)
	elif m == 2:
		print(n)
	else:
		print(n * 2)


a = int(input())
for i in range(0, a):
	(b, c) = map(int, input().split())
	if b == 1:
		print(0)
	elif b == 2:
		print(c)
	elif b % 2 != 0:
		print(c * 2)
	elif b % 2 == 0:
		print(c * 2)


t = int(input())

def get_input(line):
	result = ['', '']
	spa = False
	for letter in line:
		if letter == ' ':
			spa = True
		elif spa == False:
			result[0] += letter
		else:
			result[1] += letter
	return result
for i in range(0, t):
	ip = get_input(input())
	if int(ip[0]) == 1:
		print(0)
	elif int(ip[0]) == 2:
		print(int(ip[1]))
	else:
		print(2 * int(ip[1]))


t = int(input())
while t > 0:
	temp = list(map(int, input().split()))
	n = temp[0]
	d = temp[1]
	if n < 2:
		print(0)
	if n == 2:
		print(d)
	if n > 2:
		print(2 * d)
	t -= 1


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.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')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(N, M) = getInts()
	if N == 1:
		return 0
	if N == 2:
		return M
	return 2 * M
for _ in range(getInt()):
	print(solve())


t = int(input())
for t1 in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	if n >= 3:
		print(2 * m)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 2:
		print(k)
	if n == 1:
		print(0)
	if n > 2:
		print(2 * k)


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

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

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

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		(n, m) = MI()
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
main()


T = int(input())
for each in range(1, T + 1):
	(n, m) = input().split()
	if int(n) == 1:
		print(0)
	elif int(n) == 2:
		print(m)
	else:
		print(2 * int(m))


for t in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	else:
		ans = min(2, n - 1) * m
		print(ans)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		(n, m) = list(map(int, input().split()))
		if n == 1:
			yield 0
		elif n == 2:
			yield m
		else:
			yield (m * 2)
t = int(input())
ans = gift()
print(*ans, sep='\n')


for _ in range(int(input())):
	(arrLen, arrSum) = map(int, input().split(' '))
	if arrLen == 1:
		print(0)
		continue
	elif arrLen == 2:
		print(arrSum)
		continue
	print(arrSum * 2)


t = int(input())
for test in range(t):
	nk = input()
	nk = nk.strip()
	nk = nk.split(' ')
	nk = list(map(int, nk))
	n = nk[0]
	k = nk[1]
	if n == 2:
		print(k)
	elif n == 1:
		print(0)
	else:
		print(k * 2)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	if x > 2:
		print(2 * y)
	elif x == 2:
		print(y)
	else:
		print(0)


T = int(input())
for t in range(T):
	(N, M) = map(int, input().strip().split())
	if N == 1:
		print(0)
	elif N == 2:
		print(M)
	else:
		print(2 * M)


t = int(input())
for i in range(t):
	(n, m) = tuple(map(int, input().split(' ')))
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(2 * m)


from sys import stdin, stdout
for test in range(int(stdin.readline())):
	arr = [int(x) for x in stdin.readline().split()]
	n = arr[0]
	m = arr[1]
	if n == 1:
		stdout.write('0\n')
	elif n == 2:
		stdout.write(str(m) + '\n')
	else:
		stdout.write(str(2 * m) + '\n')


for _ in range(int(input())):
	a = input().split(' ')
	n = int(a[0])
	m = int(a[1])
	if n > 2:
		ans = 2 * m
	elif n == 2:
		ans = m
	elif n == 1:
		ans = 0
	print(ans)


from sys import stdin

def func():
	return
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


test_cases = int(input())
while test_cases > 0:
	(num, length) = map(int, input().split())
	if num == 1:
		print(0)
	elif num == 2:
		print(length)
	else:
		print(length * 2)
	test_cases -= 1


t = int(input())
while t:
	t -= 1
	a = input()
	x = a.split(' ')
	n = int(x[0])
	m = int(x[1])
	if n == 2:
		print(m)
	elif n == 1:
		print(0)
	else:
		print(m * 2)


t = int(input())
ans = []
for s in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		ans.append(0)
	elif n == 2:
		ans.append(m)
	else:
		ans.append(2 * m)
for it in ans:
	print(it)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		w = 0
		print(w)
	elif n >= 3:
		ans = 2 * m
		print(ans)
	else:
		print(m)


import math

def ans(n, m):
	if n is 1:
		return 0
	if n is 2:
		return m
	else:
		return 2 * m
t = int(input())
while t > 0:
	(n, m) = list(map(int, input().strip().split(' ')))
	print(ans(n, m))
	t -= 1


num = int(input())
for i in range(num):
	(n, k) = map(int, input().split(' '))
	if n >= 3:
		s = 2 * k
	elif n == 2:
		s = k
	else:
		s = 0
	print(s)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	(a, b, c, l, s) = (0, 0, 0, [], [])
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(k * 2)


for i in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	k1 = m // (n // 2)
	k2 = m % (n // 2)
	if n == 2:
		print(k1 + k2)
		continue
	a = []
	sum = 2 * k1 * (n // 2) + 2 * k2
	print(sum)


t = int(input())
for k in range(t):
	(a, b) = map(int, input().split())
	if a == 1:
		print(0)
	elif a > 1 and a < 3:
		print(b)
	else:
		print(b * 2)


tests = int(input())
for test in range(tests):
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


T = int(input())
for t in range(T):
	(N, M) = [int(_) for _ in input().split()]
	if N == 1:
		print(0)
		continue
	num_max = N // 2
	V = M // num_max
	R = M - V * num_max
	print((V * num_max + R) * (2 if N > 2 else 1))


p = int(input())
for i in range(p):
	arr = input().split()
	n = int(arr[0])
	m = int(arr[1])
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def solve():
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		return
	elif n == 2:
		print(m)
		return
	else:
		print(2 * m)
		return
t = int(input())
for i in range(t):
	solve()


for z in range(int(input())):
	(g, p) = map(int, input().split())
	if g == 1:
		print(0)
	if g == 2:
		print(p)
	if g != 1 and g != 2:
		print(2 * p)


import math

def BuubleSort(n, a):
	return min(n - 1, 2) * a

def main():
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ans = BuubleSort(a, b)
		print(ans)
main()


import pdb
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if a > 2:
		print(2 * b)
	elif a == 1:
		print(0)
	elif a == 2:
		print(b)


def main(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
t = int(input())
out_list = []
for i in range(t):
	(n, m) = map(int, input().split())
	out_list.append(main(n, m))
for num in out_list:
	print(num)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 10 ** 9 + 7
for _ in range(int(input())):
	(n, m) = get_ints()
	if n > 2:
		print(2 * m)
	elif n == 1:
		print(0)
	else:
		print(m)


t = int(input())
for rep in range(t):
	(n, m) = list(map(int, input().split()))
	if n >= 3:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


cases = int(input(''))
output = []
for c in range(cases):
	list1 = []
	x = input('').split()
	for i in range(2):
		list1.append(int(x[i]))
	if list1[0] == 1:
		output.append(0)
		continue
	if list1[0] == 2:
		output.append(list1[1])
		continue
	if list1[1] % 2 == 0:
		if list1[0] >= 5:
			output.append(int(int(list1[1] / 2) * 4))
		elif list1[0] == 4 or list1[0] == 3:
			output.append(list1[1] * 2)
	if int(x[1]) % 2 != 0:
		if int(x[0]) >= 5:
			output.append(int(int(list1[1]) / 2) * 2 + (int(list1[1] / 2) + 1) * 2)
		elif int(x[0]) == 4 or list1[0] == 3:
			output.append(list1[1] * 2)
print(*output, sep='\n')


def solve():
	(n, m) = map(int, input().split())
	print(min(2, n - 1) * m)
t = int(input())
for _ in range(t):
	solve()


class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: gcd(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
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 bisect import bisect_right, bisect_left
from fractions import Fraction

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans
from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = []
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(k)
	else:
		print(2 * k)


from __future__ import division, print_function
from collections import *
from math import *
from itertools import *
from time import time
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		return
	if n == 2:
		print(m)
		return
	print(m * 2)
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t:
	main()
	t -= 1


import sys, math, cmath, time
start_time = time.time()
tt = 0

def inp():
	return int(input())

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

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

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

def solve():
	(n, m) = invr()
	if n > 2:
		q = 2 * m
	elif n == 2:
		q = m
	else:
		q = 0
	print(q)

def main():
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
	sys.stdout.close()

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


from collections import defaultdict as dc
from collections import Counter
from heapq import *
import math
from bisect import bisect_left, bisect
import sys
from collections import deque as dq
from heapq import heapify, heappush, heappop
mod = 10 ** 9 + 7

def sinp():
	p = str(input())
	return p

def seive(n):
	p = [0] * (n + 1)
	for i in range(2, n):
		if p[i] == 0:
			for j in range(2 * i, n + 1, i):
				p[i] = 1
	return p

def inp():
	p = int(input())
	return p

def line():
	p = list(map(int, input().split()))
	return p

def read_mat():
	n = inp()
	a = []
	for i in range(n):
		a.append(line())
	return a

def digit(n):
	s = str(n)
	p = 0
	for i in s:
		p += int(i) ** 2
	return p

def solve(n, s):
	if n == 1:
		return 0
	if n == 2:
		return s
	return s * 2
for test in range(inp()):
	(n, s) = line()
	l = solve(n, s)
	print(l)


def readStr():
	return input()

def readInts():
	return list(map(int, readStr().split(' ')))

def main(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
(t,) = readInts()
for _ in range(t):
	(n, m) = readInts()
	x = main(n, m)
	print(x)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(x)
	else:
		print(x * 2)


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
for tt in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n <= 2:
		print(m)
	else:
		print(2 * m)


def solve(n, m):
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)
test_case = int(input())
while test_case != 0:
	(n, m) = map(int, input().split(' '))
	solve(n, m)
	test_case = test_case - 1


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

def read_ints():
	return list(map(int, input().strip().split(' ')))

def solve():
	(n, m) = read_ints()
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
T = read_int()
for _ in range(T):
	print(solve())


def unsarr(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return m * 2
test = int(input())
for i in range(test):
	(n, m) = map(int, input().split())
	print(unsarr(n, m))


t = int(input())
for i in range(t):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	if n == 1:
		print(0)
	elif n < 3 and n > 1:
		print(m)
	else:
		print(m * 2)


test = int(input())
for tes in range(test):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
		continue
	else:
		print(2 * m)


t = int(input())
for _ in range(0, t):
	s = input().split()
	(n, k) = (int(s[0]), int(s[1]))
	if n > 2:
		print(k * 2)
	elif n == 2:
		print(k)
	else:
		print(0)


for _ in range(int(input())):
	(a, b) = [int(x) for x in input().split(' ')]
	if a >= 3:
		print(2 * b)
	elif a == 1:
		print(0)
	else:
		print(b)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

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

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	t = I()
	aa = [LI() for _ in range(t)]
	rr = []
	for (n, m) in aa:
		rr.append(m * min(2, n - 1))
	return JA(rr, '\n')
print(main())


import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

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

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def SieveOfEratosthenes(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
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

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

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

def string_list():
	return list(input())

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

def binaryToDecimal(n):
	return int(n, 2)

def solve():
	(n, m) = num_input()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
t = int(input())
for _ in range(t):
	solve()


from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
for _ in range(iinput()):
	(n, m) = rinput()
	if n > 2:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


import sys

def solve():
	return None

def main():
	q = []
	for line in sys.stdin.readlines():
		q.append(line)
	for i in range(len(q)):
		q[i] = q[i].rstrip().split(' ')
		q[i] = [int(x) for x in q[i]]
	for i in range(1, len(q)):
		n = q[i][0]
		m = q[i][1]
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
main()


from sys import stdin

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

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

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	(n, m) = minput()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m + m)


t = int(input())
out = []
for i in range(t):
	[n, m] = list(map(int, input().split()))
	if n == 1:
		out.append(0)
	elif n == 2:
		out.append(m)
	else:
		out.append(2 * m)
print(*out, sep='\n')


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n >= 3:
		if m % 2 == 0:
			half = m // 2
			print(half * 4)
		else:
			half = m // 2
			halfnext = half + 1
			ans = half * 2 + halfnext * 2
			print(ans)


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

def read_ints():
	return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
	(n, m) = read_ints()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


for z in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


import sys
input = sys.stdin.buffer.readline
t = int(input())
ans = []
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		ans.append(0)
	elif n == 2:
		ans.append(m)
	else:
		ans.append(2 * m)
print('\n'.join(map(str, ans)))


from collections import deque
from collections import Counter
for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


ipnl = lambda n: [int(input()) for _ in range(n)]
inp = lambda : int(input())
ip = lambda : [int(w) for w in input().split()]
for _ in range(inp()):
	(n, m) = ip()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for _ in range(int(input())):
	(x1, y1) = map(int, input().split())
	l = []
	if x1 == 1:
		print(0)
	elif x1 == 2:
		print(y1)
	elif x1 == y1 or x1 == 3:
		print(2 * y1)
	else:
		mi = y1 // x1
		c = x1 // 2
		ans = mi * (c - 1) * 2 + (y1 - mi * (c - 1)) * 2
		print(ans)


import sys
I = sys.stdin.readline
ans = ''
for _ in range(int(I())):
	(n, m) = map(int, I().split())
	if n >= 3:
		ans += str(2 * m) + '\n'
	elif n == 1:
		ans += '0\n'
	else:
		ans += str(m) + '\n'
print(ans)


ans = ''
for _ in range(int(input())):
	a = list(map(int, input().split()))
	if a[0] == 1:
		ans += '0\n'
	elif a[0] == 2:
		ans += str(a[1]) + '\n'
	else:
		ans += str(a[1] * 2) + '\n'
print(ans)


for t in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	mod = m % n
	answer = m * 2
	print(answer)


ntests = int(input())
for _ in range(ntests):
	(tamanho, somatorio) = map(int, input().split())
	print((tamanho - 1 if tamanho < 3 else 2) * somatorio)


tests = int(input())
while tests > 0:
	(length, summary) = [int(i) for i in input().split(' ')]
	if length == 1:
		print(0)
	elif length < 3:
		print(summary)
	else:
		print(summary * 2)
	tests = tests - 1


mod = int(1000000000.0) + 7

def ip():
	return int(input())

def Ip():
	return map(int, input().split())
from math import pow
for _ in range(0, int(input())):
	(n, m) = Ip()
	ans = 0
	if n != 1:
		if n == 2:
			ans = m
		else:
			ans = m * 2
	print(ans)


from collections import Counter, defaultdict, deque
from math import *
import sys
input = sys.stdin.readline

def solve():
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
tt = int(input())
for test in range(tt):
	solve()


numTestCases = int(input())
for i in range(numTestCases):
	(n, m) = [int(num) for num in input().split(' ')]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
while t != 0:
	l = input().split()
	print(min(2, int(l[0]) - 1) * int(l[1]))
	t = t - 1


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n > 2:
		if n % 2 != 0:
			slot = n // 2
			first_item = (m - slot + 1) * 2 + (slot - 1) * 2
		if n % 2 == 0:
			slot = n // 2
			first_item = (m - slot + 1) * 2 + (slot - 1) * 2
	if n == 2:
		first_item = m
	if n == 1:
		first_item = 0
	print(first_item)


t = int(input())
ans = []
for i in range(t):
	(n, m) = map(int, input().split())
	d1 = int(m / 2)
	d2 = m - d1
	if n == 1:
		ans.append(0)
	elif n == 2:
		ans.append(m)
	else:
		ans.append(m * 2)
for i in range(t):
	print(int(ans[i]))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	(N, M) = map(int, sys.stdin.readline().split())
	if N == 1:
		print(0)
	elif N == 2:
		print(M)
	else:
		print(M * 2)


from collections import defaultdict as dd
from collections import Counter as ccd
from itertools import permutations as pp
from itertools import combinations as cc
from random import randint as rd
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq as hq
from math import gcd
mod = pow(10, 9) + 7

def inp(flag=0):
	if flag == 0:
		return list(map(int, input().strip().split(' ')))
	else:
		return int(input())
t = inp(1)
while t:
	t -= 1
	(n, m) = inp()
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	print(ans)


import sys
input = sys.stdin.readline
rInt = lambda : int(input())
mInt = lambda : list(map(int, input().split()))
rLis = lambda : list(map(int, input().split()))
t = int(input())
for _ in range(t):
	(n, m) = mInt()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def input1(type=int):
	return type(input())

def input2(type=int):
	[a, b] = list(map(type, input().split()))
	return (a, b)

def input3(type=int):
	[a, b, c] = list(map(type, input().split()))
	return (a, b, c)

def input_array(type=int):
	return list(map(type, input().split()))

def input_string():
	s = input()
	return list(s)

def main():
	t = input1(int)
	for ci in range(t):
		(n, m) = input2(int)
		if n == 1:
			print(0)
			continue
		if n == 2:
			print(m)
			continue
		pos_value_boshbe = n // 2
		na = n - 1
		if n % 2 == 0:
			pos_value_boshbe -= 1
			na -= 1
		half = m // pos_value_boshbe
		res = 0
		if pos_value_boshbe > 0:
			res += half * (na - 2)
			if m % pos_value_boshbe == 0:
				res += half * 2
			else:
				rem = m // pos_value_boshbe + m % pos_value_boshbe
				res += rem * 2
		print(res)
	return
main()


import sys
lines = sys.stdin.readlines()
t = int(lines[0])
for i in range(1, t + 1):
	(n, m) = map(int, lines[i].split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


tt = int(input())
while tt:
	tt -= 1
	l = list(map(int, input().split()))
	n = l[0]
	m = l[1]
	if n == 1:
		print(int(0))
	if n == 2:
		print(m)
	if n > 2:
		print(m * 2)


req = int(input())
ans = ''

def answer(n, m):
	res = m
	if n == 1:
		res = 0
		return res
	if n < 3:
		return res
	else:
		return m * 2
for i in range(req):
	(n, m) = map(int, input().split(' '))
	ans += str(answer(n, m)) + '\n'
print(ans.strip())


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n > 2:
		print(min(2, n - 1) * m)


def inpint():
	return int(input())

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

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

def inplist():
	return input().split()

def inpstr():
	return list(input())

def main():
	t = inpint()
	for _ in range(t):
		(n, m) = inpimap()
		if n > 2:
			print(2 * m)
		elif n == 2:
			print(m)
		else:
			print(0)
main()


test = int(input())
while test != 0:
	(n, m) = [int(x) for x in input().split()]
	print(min(2, n - 1) * m)
	test -= 1


t = int(input())
for test in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
		continue
	if n > 2:
		print(m * 2)
	else:
		print(m)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	print(min(b * 2, (a - 1) * b))


t = int(input())
for i in range(t):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	a = n - 1 if 2 > n - 1 else 2
	a *= m
	print(a)


from os import error
import sys
input = sys.stdin.readline
DEBUG = False

def inp():
	return int(input())

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

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

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

def solve():
	(n, m) = inlt()
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
n = inp()
for t in range(n):
	ans = solve()
	print(ans)


def solve():
	(n, m) = map(int, input().split(' '))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)

def __starting_point():
	t = int(input())
	for tc in range(t):
		solve()
__starting_point()


t = int(input())
for i in range(0, t):
	a = input().split()
	n = eval(a[0])
	m = eval(a[1])
	if n == 0 or n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


def fcnt():
	result = ''
	for _ in range(int(input())):
		(n, m) = map(int, input().split())
		if n == 1:
			result += '0\n'
		elif n == 2:
			result += str(m) + '\n'
		else:
			result += str(m * 2) + '\n'
	print(result[:-1])
fcnt()


import sys
ints = (int(x) for x in sys.stdin.read().split())
sys.setrecursionlimit(3000)

def main():
	ntc = next(ints)
	for tc in range(1, ntc + 1):
		(n, m) = (next(ints) for i in range(2))
		ans = 0 if n == 1 else m if n == 2 else 2 * m
		print(ans)
	return
main()


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
BUFSIZE = 8192
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():
	t = int(input())
	for i in range(t):
		nm = list(map(int, input().split()))
		(n, m) = (nm[0], nm[1])
		if n == 1:
			print(0)
			continue
		if n == 2:
			print(m)
			continue
		print(2 * m)
main()


T = int(input())
for _ in range(T):
	(n, m) = map(int, input().split())
	ans = 0
	if n == 2:
		ans = m
	if n > 2:
		ans = m * 2
	print(ans)


import sys
cases = int(sys.stdin.readline())
for _ in range(cases):
	(n, m) = map(int, sys.stdin.readline().split())
	if n <= 3:
		print((n - 1) * m)
	else:
		print(2 * m)


import math
from collections import deque
import sys
sys.setrecursionlimit(10 ** 4)

def Divisors(n):
	l = []
	i = 2
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
		i = i + 1
	return l

def SieveOfEratosthenes(n):
	l = []
	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
	for p in range(2, n + 1):
		if prime[p]:
			l.append(p)
	return l

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(n)
	return l

def Factors(n):
	result = []
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			if i == n / i:
				result.append(i)
			else:
				result.append(i)
				result.append(n // i)
	result.append(1)
	result.append(n)
	return result

def maxSubArraySum(a):
	max_so_far = 0
	max_ending_here = 0
	size = len(a)
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < abs(max_ending_here):
			max_so_far = max_ending_here
	return max_so_far

def longestsubarray(arr, n, k):
	current_count = 0
	max_count = 0
	for i in range(0, n, 1):
		if arr[i] % k != 0:
			current_count += 1
		else:
			current_count = 0
		max_count = max(current_count, max_count)
	return max_count

def main():
	(n, x) = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(x)
	else:
		print(2 * x)
t = int(input())
for i in range(0, t):
	main()


tests = int(input())
for i in range(0, tests):
	(lenth, summ) = map(int, input().split())
	if lenth == 1:
		print(0)
	elif lenth == 2:
		print(summ)
	else:
		print(summ * 2)


import math
for i in range(int(input())):
	(k, n) = map(int, input().split())
	if k >= 3:
		print(2 * n)
	elif k == 2:
		print(n)
	else:
		print(0)


import math
for _ in range(int(input())):
	(n, s) = map(int, input().strip().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(s)
	else:
		min_val = s // 2
		max_val = math.ceil(s / 2)
		print(min_val * 2 + max_val * 2)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	if l == 1:
		print(0)
	elif l == 2:
		print(r)
	else:
		print(2 * r)


T = int(input())
for i in range(T):
	(X, Y) = list(map(int, input().split()))
	if X == 1:
		print(0)
	elif X == 2:
		print(Y)
	else:
		print(2 * Y)


from collections import defaultdict, deque
from itertools import permutations
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from copy import deepcopy
import heapq
int_input = lambda : int(stdin.readline())
string_input = lambda : stdin.readline().split()
multi_int_input = lambda : map(int, stdin.readline().split())
multi_input = lambda : stdin.readline().split()
list_input = lambda : list(map(int, stdin.readline().split()))
string_list_input = lambda : list(string_input())
MOD = pow(10, 9) + 7
test = int_input()
for _ in range(test):
	(n, m) = multi_int_input()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def solution(n, m):
	if n > 2:
		print(2 * m)
		return
	if n == 2:
		print(m)
		return
	if n == 1:
		print(0)
		return
inp = int(input())
for _ in range(inp):
	(n, m) = tuple(map(int, input().split()))
	solution(n, m)


def main():
	for _ in range(int(input())):
		(n, m) = list(map(int, input().split()))
		print(0 if n == 1 else m if n == 2 else 2 * m)
main()


t = int(input())
for j in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print('0')
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


def main():
	T = int(input())
	for _ in range(T):
		(N, M) = list(map(int, input().split()))
		if N > 2:
			print(2 * M)
		elif N == 2:
			print(M)
		else:
			print(0)
main()


import math
total_case = int(input())
for i in range(total_case):
	aline = input().split()
	(n, m) = [int(x) for x in aline]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

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

def si():
	return input()

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

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

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 swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	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 phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

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

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

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

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	(n, m) = read()
	if n == 1:
		print(0)
		return
	if n == 2:
		print(m)
		return
	if n > 2:
		print(2 * m)
		return
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(scanner()):
	solve()


a = int(input())
for _ in range(a):
	(n, k) = [int(x) for x in input().split()]
	if n == 1:
		print('0')
	elif n == 2:
		print(k)
	else:
		print(2 * k)


t = int(input())
for x in range(t):
	L = list(map(int, input().strip().split(' ')))
	n = L[0]
	m = L[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
while t:
	try:
		(n, m) = list(map(int, input().split()))
	except EOFError:
		break
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import math
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	(l, k) = (math.floor(m / 2), math.ceil(m / 2))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n == 3:
		print(m * 2)
	elif n == 4:
		print(m * 2)
	else:
		print(k * 2 + l * 2)


t = int(input())
answers = []
for i in range(t):
	(n, m) = list(map(int, input().split(' ')))
	if n == 1:
		answers.append(0)
	elif n == 2:
		answers.append(m)
	elif n > 2:
		answers.append(m * 2)
for i in answers:
	print(i)


import sys
import math
from collections import Counter, defaultdict
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
t = int(input())
while t != 0:
	t -= 1
	(m, n) = [int(x) for x in input().strip().split()]
	if m == 1:
		print(0)
	elif m == 2:
		print(n)
	else:
		print(2 * n)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, Counter
import math

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

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
t = int(input())
while t > 0:
	t -= 1
	(n, m) = listIn()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import os
import sys
from io import BytesIO, IOBase

def solution(n, m):
	if n == 1:
		write(0)
		return
	if n == 2:
		write(m)
		return
	write(2 * m)

def main():
	for _ in range(r_int()):
		(n, m) = r_array()
		solution(n, m)
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args[:-1]:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(str(args[-1]))
	sys.stdout.write(end)

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

def r_int():
	return int(input())
main()


t = int(input())
a = []
for i in range(t):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		sum = 0
	elif n == 2:
		sum = m
	else:
		sum = 2 * m
	a.append(sum)
for x in a:
	print(x)


def solve():
	(n, m) = list(map(int, input().split()))
	return min(2, n - 1) * m

def main():
	t = int(input())
	while t:
		print(solve())
		t -= 1
main()


import random
t = int(input())
n_m = []
for i in range(t):
	n_m.append(tuple(map(int, input().split(' '))))
for i in n_m:
	if i[0] == 1:
		print(0)
	elif i[0] == 2:
		print(i[1])
	else:
		print(i[1] * 2)


n = int(input())
ans = []
for i in range(n):
	t = input().split()
	t = [int(x) for x in t]
	ans.append(min(2, t[0] - 1) * t[1])
for j in ans:
	print(j)


import math
import sys
input = sys.stdin.readline

def inp():
	return int(input())

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

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

def invr():
	return map(int, input().split())
test_cases = inp()
for _ in range(test_cases):
	(length, sum) = map(int, input().split())
	if length == 1:
		print(0)
	elif length == 2:
		print(sum)
	else:
		print(int(2 * sum))


for nt in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(2 * m)


import math
t = int(input())
while t:
	t -= 1
	(n, m) = list(map(int, input().split()))
	if n <= 1:
		print(0)
		continue
	greedy_num = m
	if n > 2:
		print(greedy_num * 2)
	else:
		print(greedy_num)


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')

def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
for i in range(int_in()):
	(n, m) = arr_in()
	out(solve(n, m))


import sys
import bisect
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	(n, m) = Ri()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


from collections import Counter
import math

def solve():
	(m, n) = map(int, input().split())
	if m == 1:
		print(0)
		return
	elif m == 2:
		print(n)
		return
	else:
		print(2 * n)
		return
for _ in range(int(input())):
	solve()


T = int(input())
for t in range(T):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n >= 3:
		print(2 * m)


t = int(input())
while t > 0:
	t -= 1
	inp = input().split(' ')
	inp = list(map(int, inp))
	n = inp[0]
	k = inp[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


try:
	for i in range(int(input())):
		(n, m) = map(int, input().split())
		print(min(m * 2, (n - 1) * m))
except:
	pass


t = int(input())
for i in range(t):
	inps = list(map(lambda x: int(x), input().split()))
	(n, m) = (inps[0], inps[1])
	if n == 0:
		print(0)
	elif n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	plcs = n // 2
	if n % 2 == 0:
		plcs -= 1
	if plcs > m:
		return m * 2
	each = m // plcs
	extr = m % plcs
	ans = each * (plcs - 1) + (each + extr)
	return ans * 2
for _ in range(int(input())):
	(n, m) = map(int, input().strip().split(' '))
	print(solve(n, m))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nlargest, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, log
from collections import defaultdict as dd, deque, Counter as C, OrderedDict as od
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353
inf = int(1e+20)

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	(n, m) = sp()
	if n == 1:
		out(0)
		continue
	if n == 2:
		out(m)
		continue
	out(m * 2)


n = int(input())
a = []
for i in range(n):
	(b, c) = map(int, input().split())
	if b == 0:
		a.append(0)
	elif b == 1:
		a.append(0)
	elif b == 2:
		a.append(c)
	else:
		a.append(2 * c)
for i in range(len(a)):
	print(a[i])


import sys
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def printwsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')

def printchk(*args):
	return print(*args, end='tst, ')
MODPRIME = int(1000000000.0 + 7)
BABYMOD = 998244353
for _testcases_ in range(int(input())):
	(n, m) = get_ints()
	ans = 0
	if n <= 1:
		ans = 0
	if n == 2:
		ans = m
	if n > 2:
		ans = 2 * m
	print(ans)


N = int(input())
answers = []
for i in range(N):
	(n, m) = map(int, input().split())
	if n == 1:
		answers.append(0)
	elif n == 2:
		answers.append(m)
	else:
		answers.append(m * 2)
for i in answers:
	print(i)


from collections import Counter
t = int(input())
while t > 0:
	t = t - 1
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
import math
for i in range(t):
	(n, k) = map(int, input().strip().split())
	(n, k) = (k, n)
	if k == 1:
		print(0)
	elif k == 2:
		print(n)
	else:
		print(2 * n)


test_case = int(input())
my_list = []
for i in range(test_case):
	my_list.append([int(j) for j in input().split()])
for j in range(test_case):
	if my_list[j][0] == 1:
		print(0)
	elif my_list[j][0] == 2:
		print(my_list[j][1])
	else:
		print(my_list[j][1] * 2)


def solve():
	(n, m) = [int(i) for i in input().split()]
	if n > 2:
		return m * 2
	if n == 2:
		return m
	return 0
t = int(input())
ans = []
while t:
	ans.append(str(solve()))
	t -= 1
print('\n'.join(ans))


t = int(input())
for i in range(t):
	nm = list(map(int, input().split()))
	n = nm[0]
	m = nm[1]
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
		continue
	elif n == 3:
		print(m * 2)
		continue
	elif n == 4:
		print(m * 2)
		continue
	else:
		middle = m // 2
		rest = m - middle
		print(rest * 2 + middle * 2)
		continue


k = int(input(''))
list = []
for i in range(0, k):
	(n, m) = input('').split()
	n = int(n)
	m = int(m)
	if n == 1:
		list.append(0)
	elif n == 2:
		list.append(m)
	else:
		list.append(2 * m)
for i in range(0, k):
	print(list[i])


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	w = x // 2
	out = 0
	if x == 1:
		out = 0
	elif x == 2:
		out = y
	else:
		out = y * 2
	print(out)


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	if n == 1:
		print('0')
	elif n == 2:
		print(k)
	else:
		print(2 * k)


def ri():
	return int(input())

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

def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m

def main():
	for _ in range(ri()):
		(n, m) = ria()
		print(solve(n, m))
main()


def call():
	(n, k) = map(int, input().split())
	print(min(2, n - 1) * k)
t = int(input())
for _ in range(t):
	call()


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby

def Golf():
	(*a,) = map(int, open(0))

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in range(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def bit_combination(n, base=2):
	rt = []
	for tb in range(base ** n):
		s = [tb // base ** bt % base for bt in range(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
YN = ['YES', 'NO']
Yn = ['Yes', 'No']
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(1, 0), (-1, 0), (0, 1), (0, -1)]
EightNb = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('EWNS', FourNb))
l_alp = string.ascii_lowercase
input = lambda : sys.stdin.readline().rstrip()

class Tree:

	def __init__(self, inp_size=None, ls=None, init=True, index=0):
		self.LCA_init_stat = False
		self.ETtable = []
		if init:
			if ls == None:
				self.stdin(inp_size, index=index)
			else:
				self.size = len(ls) + 1
				(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def stdin(self, inp_size=None, index=1):
		if inp_size == None:
			self.size = int(input())
		else:
			self.size = inp_size
		(self.edges, _) = GI(self.size, self.size - 1, index=index)
		return

	def listin(self, ls, index=0):
		self.size = len(ls) + 1
		(self.edges, _) = GI(self.size, self.size - 1, ls, index=index)
		return

	def __str__(self):
		return str(self.edges)

	def dfs(self, x, func=lambda prv, nx, dist: prv + dist, root_v=0):
		q = deque()
		q.append(x)
		v = [-1] * self.size
		v[x] = root_v
		while q:
			c = q.pop()
			for (nb, d) in self.edges[c]:
				if v[nb] == -1:
					q.append(nb)
					v[nb] = func(v[c], nb, d)
		return v

	def EulerTour(self, x):
		q = deque()
		q.append(x)
		self.depth = [None] * self.size
		self.depth[x] = 0
		self.ETtable = []
		self.ETdepth = []
		self.ETin = [-1] * self.size
		self.ETout = [-1] * self.size
		cnt = 0
		while q:
			c = q.pop()
			if c < 0:
				ce = ~c
			else:
				ce = c
				for (nb, d) in self.edges[ce]:
					if self.depth[nb] == None:
						q.append(~ce)
						q.append(nb)
						self.depth[nb] = self.depth[ce] + 1
			self.ETtable.append(ce)
			self.ETdepth.append(self.depth[ce])
			if self.ETin[ce] == -1:
				self.ETin[ce] = cnt
			else:
				self.ETout[ce] = cnt
			cnt += 1
		return

	def LCA_init(self, root):
		self.EulerTour(root)
		self.st = SparseTable(self.ETdepth, init_func=min, init_idl=inf)
		self.LCA_init_stat = True
		return

	def LCA(self, root, x, y):
		if self.LCA_init_stat == False:
			self.LCA_init(root)
		(xin, xout) = (self.ETin[x], self.ETout[x])
		(yin, yout) = (self.ETin[y], self.ETout[y])
		a = min(xin, yin)
		b = max(xout, yout, xin, yin)
		id_of_min_dep_in_et = self.st.query_id(a, b + 1)
		return self.ETtable[id_of_min_dep_in_et]

class SparseTable:

	def __init__(self, ls, init_func=min, init_idl=float('inf')):
		self.func = init_func
		self.idl = init_idl
		self.size = len(ls)
		self.N0 = self.size.bit_length()
		self.table = [ls[:]]
		self.index = [list(range(self.size))]
		self.lg = [0] * (self.size + 1)
		for i in range(2, self.size + 1):
			self.lg[i] = self.lg[i >> 1] + 1
		for i in range(self.N0):
			tmp = [self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) for j in range(self.size)]
			tmp_id = [self.index[i][j] if self.table[i][j] == self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) else self.index[i][min(j + (1 << i), self.size - 1)] for j in range(self.size)]
			self.table += [tmp]
			self.index += [tmp_id]

	def query(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		return self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))])

	def query_id(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		(a, b) = (self.index[N][l], self.index[N][max(0, r - (1 << N))])
		if self.table[0][a] == self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))]):
			b = a
		return b

	def __str__(self):
		return str(self.table[0])

	def print(self):
		for i in self.table:
			print(*i)

class Comb:

	def __init__(self, n, mo=10 ** 9 + 7):
		self.fac = [0] * (n + 1)
		self.inv = [1] * (n + 1)
		self.fac[0] = 1
		self.fact(n)
		for i in range(1, n + 1):
			self.fac[i] = i * self.fac[i - 1] % mo
			self.inv[n] *= i
			self.inv[n] %= mo
		self.inv[n] = pow(self.inv[n], mo - 2, mo)
		for i in range(1, n):
			self.inv[n - i] = self.inv[n - i + 1] * (n - i + 1) % mo
		return

	def fact(self, n):
		return self.fac[n]

	def invf(self, n):
		return self.inv[n]

	def comb(self, x, y):
		if y < 0 or y > x:
			return 0
		return self.fac[x] * self.inv[x - y] * self.inv[y] % mo
show_flg = False
show_flg = True
ans = 0
T = I()
for _ in range(T):
	ans = 0
	(n, m) = LI()
	ans = m * 2
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	print(ans)


import sys
input = sys.stdin.readline
from collections import defaultdict as dd
t = int(input())
while t:
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
	t -= 1


import math
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	ans = 0
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	elif n > 2:
		ans = 2 * m
	print(ans)


t = int(input())
for q in range(t):
	ch = input()
	L = [int(i) for i in ch.split()]
	(n, m) = (L[0], L[1])
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


def solve():
	size_and_sum = list(map(int, input().split()))
	if size_and_sum[0] <= 1:
		print(0)
	elif size_and_sum[0] == 2:
		print(size_and_sum[1])
	else:
		print(2 * size_and_sum[1])

def main():
	for case in range(int(input())):
		solve()
main()


def solution(t):
	for _ in range(t):
		nm = input().split()
		(n, m) = (int(nm[0]), int(nm[1]))
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
solution(int(input()))


import sys

def process(n, m):
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
input = sys.stdin.readline

def inp():
	return int(input())

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

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

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

def f(a, n):
	return True

def is_permutation(arr):
	for e in range(1, len(arr) + 1):
		if e not in arr:
			return False
	return True
t = inp()
for i in range(t):
	nm = inlt()
	(n, m) = (nm[0], nm[1])
	process(n, m)


rw = int(input())
for ewqr in range(rw):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	if n == 3:
		print(2 * m)
		continue
	if m == 1:
		print(2)
		continue
	if n == 4:
		print(2 * m)
		continue
	ans = 2 * m
	print(ans)


import math
p = input()
t = int(p)
for k in range(0, t, 1):
	import array
	arr1 = array.array('l', [])
	arr1 = [int(i) for i in input().split()]
	n = arr1[0]
	s = arr1[1]
	if n <= 1:
		print('0')
	elif n == 2:
		print(s)
	else:
		aa = int(2 * s)
		print(aa)


c = int(input())
for z in range(c):
	l = [int(i) for i in input().split(' ')]
	if l[0] == 1:
		print(0)
	elif l[0] == 2:
		print(l[1])
	else:
		print(l[1] * 2)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		vals = n // 2
		val = m // vals
		ct = val * (vals - 1) + (m - val * (vals - 1))
		if n % 2:
			print(ct * 2)
		else:
			ct = val * (vals - 1)
			ct += m - ct
			print(ct * 2)


t = int(input())
for i in range(t):
	temp = input().split()
	n = int(temp[0])
	su = int(temp[1])
	if n == 1:
		print(0)
	elif n == 2:
		print(su)
	else:
		print(2 * su)


import sys
import math
from collections import defaultdict
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
for t in range(int(input())):
	(n, m) = intInp()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def getN():
	return int(input())

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

def getList():
	return list(map(int, input().split()))
from collections import defaultdict, deque
from sys import exit
import math
import copy
from bisect import bisect_left
import sys
INF = 10 ** 17
MOD = 998244353

def solve():
	(a, b) = getList()
	if a == 1:
		print(0)
		return
	elif a == 2:
		print(b)
		return
	else:
		print(b * 2)
		return

def main():
	n = getN()
	for _ in range(n):
		solve()
main()


import math
t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	print(min(2, n - 1) * m)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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 value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]
for _ in range(int(input())):
	(n, m) = value()
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(2 * m)


import heapq as hp
import math

def primeSeive(n):
	l = [True] * (n + 1)
	l[0] = l[1] = False
	l[2] = l[3] = True
	for i in range(4, n + 1, 2):
		l[i] = False
	for i in range(9, n + 1, 3):
		l[i] = False
	for i in range(10, n + 1, 5):
		l[i] = False
	for i in range(7, int(math.sqrt(n)) + 1, 2):
		if l[i]:
			for j in range(i * i, n + 1, i):
				l[j] = False
	return [i for i in range(n + 1) if l[i]]

def unpack(func=int):
	return map(func, input().split())

def range_n():
	return range(int(input()))

def main():
	for _ in range_n():
		(n, m) = unpack()
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
main()


for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		a = n // 2
		b = m // a
		c = m % a
		d = a - c
		print(b * d * 2 + (b + 1) * c * 2)


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	n = I()
	ans = []
	for _ in range(n):
		(a, b) = LI()
		if a == 1:
			ans.append(0)
		elif a == 2:
			ans.append(b)
		else:
			ans.append(b * 2)
	for x in ans:
		print(x)
main()


t = int(input())
answers = []
for _ in range(t):
	(n, m) = [int(i) for i in input().split()]
	if n == 1:
		answers.append(0)
	elif n == 2:
		answers.append(m)
	else:
		answers.append(2 * m)
print(*answers, sep='\n')


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	(n, m) = mapi()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n & 1:
		print(m // (n // 2) * 2 * (n // 2 - 1) + 2 * (m - m // (n // 2) * (n // 2 - 1)))
	else:
		print(m // (n // 2) * 2 * (n // 2 - 1) + 2 * (m - m // (n // 2) * (n // 2 - 1)))


t = int(input())
for _ in range(t):
	(a, m) = map(int, input().split(' '))
	if a < 2:
		print(0)
		continue
	if a == 2:
		print(m)
	else:
		print(m * 2)


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math
import math
import time

def bin1(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = k ** mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin1(mid + 1, r, k, t, b, val, ans)
		else:
			return bin1(l, mid - 1, k, t, b, val, ans)

def bin2(l, r, k, t, b, val, ans):
	if l > r:
		return ans
	else:
		mid = (l + r) // 2
		v = t * k ** mid + b * mid
		if v == val:
			return v
		elif v > val:
			ans = mid
			return bin2(l, mid - 1, k, t, b, val, ans)
		else:
			return bin2(mid + 1, r, k, t, b, val, ans)

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

def bin(l, r, ll, val):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if val >= ll[mid][0] and val <= ll[mid][1]:
			return mid
		elif val < ll[mid][0]:
			return bin(l, mid - 1, ll, val)
		else:
			return bin(mid + 1, r, ll, val)

def deci(n):
	s = ''
	while n != 0:
		if n % 2 == 0:
			n = n // 2
			s = '0' + s
		else:
			n = n // 2
			s = '1' + s
	return s

def diff(s1, s2):
	if len(s1) < len(s2):
		v = len(s1)
		while v != len(s2):
			s1 = '0' + s1
			v = v + 1
	else:
		v = len(s2)
		while v != len(s1):
			s2 = '0' + s2
			v = v + 1
	c = 0
	for i in range(len(s1)):
		if s1[i:i + 1] != s2[i:i + 1]:
			c = c + 1
	return c
from sys import stdin, stdout

def fac(a, b):
	v = a
	while a != b:
		v *= a - 1
		a = a - 1
	return v

def bino(l, r, n):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		val1 = math.log(n / mid + 1, 2)
		val2 = int(val1)
		if val1 == val2:
			return val1
		elif val1 < 1.0:
			return bino(l, mid - 1, n)
		else:
			return bino(mid + 1, r, n)

def binary(l, r, val, ans):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		v = val ** 5 - mid ** 5
		print(v)
		if v == ans:
			print('HOHOHOH')
			return mid
		elif v < ans:
			return binary(mid + 1, r, val, ans)
		else:
			return binary(l, mid - 1, val, ans)

def check(n):
	v = 1
	while n != 0:
		if n % 10 == 0 or n % 10 == 1:
			n = n // 10
		else:
			v = 0
			break
	return v

def stairs(n, m, val, c, l):
	if val > n:
		return 100000
	elif val == n:
		if l[val] != -1:
			return l[val]
		elif c % m == 0:
			l[val] = c
			return l[val]
		else:
			l[val] = 100000
			return l[val]
	else:
		l[val] = min(stairs(n, m, val + 1, c + 1, l), stairs(n, m, val + 2, c + 1, l))
		return l[val]

def fact(n):
	if n == 0 or n == 1:
		return 1
	else:
		return n * fact(n - 1)

def some(n):
	s = 0
	while n != 0:
		s += fact(n % 10)
		n //= 10
	return s

def bin(l, r, val, ll):
	if l > r:
		return -1
	else:
		mid = (l + r) // 2
		if ll[mid] == val:
			return mid
		elif ll[mid] > val:
			return bin(l, mid - 1, val, ll)
		else:
			return bin(mid + 1, r, val, ll)

def fn(n):
	c = -1
	for i in range(2, int(sqrt(n)) + 1):
		if n % 2 == 0:
			c = i
			break
	if c == -1:
		c = n
	return c
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split(' '))
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


def int_input():
	return int(input())

def ints_input():
	return (int(x) for x in input().split())
t = int_input()
for _ in range(t):
	(n, m) = ints_input()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m + m)


t = int(input())
otv = list()
str = list()
for i in range(t):
	str = input().split(' ')
	n = int(str[0])
	m = int(str[1])
	if n == 1:
		otv.append(0)
	if n == 2:
		otv.append(m)
	if n > 2:
		otv.append(2 * m)
	str.clear()
for i in otv:
	print(i)


T = int(input())
for _ in range(T):
	_nm = input().split()
	n = int(_nm[0])
	m = int(_nm[1])
	if n <= 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def main():
	from sys import stdin
	from sys import stdout
	T = int(stdin.readline())
	for t in range(T):
		(n, m) = list(map(int, stdin.readline().split(' ')))
		ans = get_ans(n, m)
		if t < T - 1:
			stdout.write(ans + '\n')
		else:
			stdout.write(ans + '\n')

def get_ans(n, m):
	if n == 1:
		s = 0
	elif n == 2:
		s = m
	else:
		s = 2 * m
	return str(s)
main()


for x in range(int(input())):
	(m, n) = map(int, input().split())
	if m == 1:
		print(0)
	elif m >= 3:
		print(n * 2)
	else:
		print(n)


n1 = int(input())
for x in range(n1):
	n2 = input().split(' ')
	a = int(n2[0])
	b = int(n2[1])
	print(min(2, a - 1) * b)


t = input()
t = int(t)
while t > 0:
	line = input()
	i = line.split()
	x = int(i[0])
	y = int(i[1])
	if x == 1:
		print('0')
	elif x == 2:
		print(y)
	else:
		print(y * 2)
	t = t - 1


import sys, os

def f():
	args = list(map(int, input().split()))
	if len(args) > 1:
		return args
	return args[0]
if 'local' in os.environ:
	sys.stdin = open('./input.txt', 'r')

def solve():
	t = f()
	for i in range(t):
		(n, m) = f()
		if n == 1:
			print(0)
			continue
		if n == 2:
			print(m)
			continue
		print(2 * m)
solve()


n = int(input())
for i in range(n):
	(l, s) = input().split()
	l = int(l)
	s = int(s)
	if l == 1:
		print(0)
	elif l == 2:
		print(s)
	else:
		print(2 * s)


t = int(input())
i = 0
while i < t:
	numList = list(map(int, input().strip().split()))[:2]
	if numList[0] == 1:
		print(0)
	elif numList[0] == 2:
		print(numList[1])
	else:
		print(2 * numList[1])
	i += 1


import sys

def process():
	(N, M) = [int(s) for s in sys.stdin.readline().split()]
	if N < 2:
		print(0)
	elif N == 2:
		print(M)
	else:
		print(M * 2)
T = int(sys.stdin.readline())
for x in range(T):
	process()


from collections import defaultdict, deque
import heapq, bisect, math, itertools, string, queue, copy, time
import sys
input = sys.stdin.readline
INF = float('inf')
mod = 10 ** 9 + 7
eps = 10 ** (-7)

def inp():
	return int(input())

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

def inpl_str():
	return list(input().split())
t = inp()
for i in range(t):
	(n, m) = inpl()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


n = int(input())
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if a < 2:
		print('0')
	elif a == 2:
		print(b)
	else:
		print(b * 2)


import math
import atexit
import io
import sys
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def array_to_string(a):
	s = ''
	for i in a:
		s += str(i) + ' '
	return s[0:len(s) - 1]

def isPrime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return 0
	return 1

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)
t = int(input())
while t > 0:
	t = t - 1
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n > 2:
		print(m * 2)
	else:
		print(m)


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

def solve():
	(n, m) = RA()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
t = int(input())
for i in range(t):
	solve()


def ans(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
t = int(input())
L = []
for i in range(t):
	(n, m) = map(int, input().split())
	L.append(ans(n, m))
for l in L:
	print(l)


inp = lambda : map(int, input().split())
t = int(input())
for i in range(t):
	(n, m) = inp()
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
		continue
	else:
		print(2 * m)


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

def read_ints():
	ints = [int(x) for x in input().split(' ')]
	return ints

def solve(n, m):
	if n <= 1:
		return 0
	elif n == 2:
		return m
	else:
		return m * 2
t = read_int()
for case in range(t):
	(n, m) = read_ints()
	sol = solve(n, m)
	print(sol)


t = int(input())
w = []
for i in range(t):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	if n % 2 == 1 and n > 1:
		k = n // 2
		s = 2 * k * (m // k) + 2 * (m % k)
		w.append(s)
	elif n == 1:
		s = 0
		w.append(s)
	elif n == 2:
		w.append(m)
	else:
		r = n - 1
		k = r // 2
		s = 2 * k * (m // k) + 2 * (m % k)
		w.append(s)
for t in w:
	print(t)


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	(n, m) = rl()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def ab():
	(b, c) = map(int, input().split())
	if b == 1:
		print(0)
		return
	if b == 2:
		print(c)
		return
	else:
		print(c * 2)
		return
for i in range(0, int(input())):
	ab()


import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

def main():
	(N, M) = map(int, input().split())
	if N == 1:
		return 0
	numtri = (N - 1) // 2
	remain = (N - 1) % 2
	ans = 0
	if numtri:
		ans += M // numtri * numtri * 2
		ans += M % numtri * 2
	else:
		ans = M
	return ans
t = int(input())
for _ in range(t):
	print(main())


def sum_of_primes():
	a = input()
	l = []
	for i in range(int(a)):
		b = input()
		b = [int(i) for i in b.split(' ')]
		n = b[0]
		m = b[1]
		if n == 1:
			diff = 0
			l.append(diff)
			continue
		if n == 2:
			diff = m
			l.append(diff)
			continue
		if n % 2 == 0:
			divider = n // 2
		else:
			divider = (n - 1) // 2
		remainder_m_n = m % divider
		normal_item = m // divider
		the_mid_item = normal_item + remainder_m_n
		if n % 2 == 0:
			diff = the_mid_item * 2 + normal_item * (divider - 3) * 2 + 2 * normal_item * 2
		else:
			diff = the_mid_item * 2 + normal_item * (divider - 1) * 2
		l.append(diff)
	for i in l:
		print(i)
sum_of_primes()


for tc in range(int(input())):
	(n, m) = [int(_) for _ in input().split()]
	print(2 * m if n > 2 else m if n == 2 else 0)


t = int(input())
import math
delay = []
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def count():
	(n, m) = list(map(int, input().split()))
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return m * 2
for i in range(int(input())):
	print(count())


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

def si():
	return input()

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

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n
for _ in range(int(input())):
	(n, m) = mi()
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import sys, math
input = lambda : sys.stdin.readline().rstrip()
for i in range(int(input())):
	(n, m) = map(int, input().split())
	if n < 2:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def fun(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	if n != 2 and n == m:
		return 2 * m
	else:
		return 2 * m
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	ans = fun(n, m)
	print(ans)


def func(n):
	for i in range(n):
		k = list(map(int, input().split()))
		print(min(2, k[0] - 1) * k[1])
n = int(input())
func(n)


import os
from math import ceil
import sys
from io import BytesIO, IOBase
import sys, bisect, copyreg, copy, statistics, os
import random

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

def IIX():
	return (int(x) for x in sys.stdin.readline().split())

def II():
	return int(inp())

def LI():
	return list(map(int, inp().split()))

def LS():
	return list(map(str, inp().split()))

def L(x):
	return list(x)

def out(var):
	return sys.stdout.write(str(var))

class GraphAL:

	def __init__(self, Nodes, isDirected=False):
		self.nodes = [x for x in range(1, Nodes + 1)]
		self.adj_list = {}
		self.isDirected = isDirected
		for node in self.nodes:
			self.adj_list[node] = []

	def add_edge(self, x, y):
		self.adj_list[x].append(y)
		if self.isDirected == False:
			self.adj_list[y].append(x)

	def return_graph(self):
		return self.adj_list

class GraphAM:

	def __init__(self, Nodes, isDirected=False):
		self.adj_matrix = [[0] * (Nodes + 1) for x in range(Nodes + 2)]
		self.isDirected = isDirected

	def add_edge(self, x, y):
		if self.isDirected:
			self.adj_matrix[x][y] = 1
		elif self.isDirected == False:
			self.adj_matrix[x][y] = 1
			self.adj_matrix[y][x] = 1

	def delete_edge(self, x, y):
		if self.isDirected:
			self.adj_matrix[x][y] = 0
		if self.isDirected == False:
			self.adj_matrix[x][y] = 0
			self.adj_matrix[y][x] = 0

	def degree(self, x):
		l = self.adj_matrix[x]
		return l.count(1)

	def return_graph(self):
		return self.adj_matrix

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def solve():
	(n, m) = IIX()
	arr = []
	flag = False
	print(min(2, n - 1) * m)
	return

def recur(n, i):
	return
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.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
try:
	t = II()
	for _ in range(t):
		solve()
except EOFError as e:
	out('')
except RuntimeError as r:
	out('')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	if n % 2 == 0:
		n_ = n // 2
		(q, r) = divmod(m, n_)
		ans = q * (n_ * 2 - 1)
		ans += 2 * r
		print(max(ans, 2 * m))
	else:
		n_ = (n - 1) // 2
		(q, r) = divmod(m, n_)
		ans = q * (n_ * 2)
		ans += 2 * r
		print(max(ans, 2 * m))


t = int(input())
op = []
for _ in range(t):
	(n, m) = list(map(int, input().split(' ')))
	if n == 1:
		op.append(0)
	elif n == 2:
		op.append(m)
	else:
		op.append(2 * m)
for i in op:
	print(i)


import math
respostas = []
test = int(input())
for x in range(test):
	(n, m) = map(int, input().split())
	if n == 1:
		respostas.append(0)
	elif n == 2:
		respostas.append(m)
	else:
		resp = min(2, n - 1) * m
		respostas.append(resp)
for x in respostas:
	print(x)


import sys
import math
data = sys.stdin.readlines()
for i in range(int(data[0])):
	a = list(map(int, data[i + 1].split(' ')))
	n = a[0]
	m = a[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def solve(time):
	(n, m) = [int(x) for x in input().split()]
	if n == 2:
		print(m)
	elif n == 1:
		print(0)
	else:
		print(m * 2)
nums = int(input())
for _ in range(1, nums + 1):
	solve(_)


def Most_unstable(n, m):
	if n == 1:
		return 0
	elif n - 2 > 0:
		return m * 2
	else:
		return m
t = int(input())
for i in range(t):
	line = input().rstrip().split()
	n = int(line[0])
	m = int(line[1])
	ans = Most_unstable(n, m)
	print(ans)
	print('\n')


import math
import os
import random
import re
import sys

def algo(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	print(algo(n, m))


def pinox(n, m):
	if n >= 3:
		return 2 * m
	if n == 2:
		return m
	if n == 1:
		return 0
for _ in [0] * int(input()):
	(a, b) = [int(i) for i in input().split()]
	print(pinox(a, b))


from sys import stdin
input = stdin.readline
for Ti in range(int(input().strip())):
	(n, m) = [int(x) for x in input().strip().split()]
	ans = 0
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	print(ans)


def compute(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return m * 2
q = int(input())
for _ in range(q):
	(n, m) = map(int, input().split())
	print(compute(n, m))


t = int(input())
for i in range(0, t):
	(n, m) = map(int, input().split())
	p = m - 1
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
		continue
	elif n >= 3:
		print(m + m)
		continue


t = int(input())
while t > 0:
	t -= 1
	(n, s) = [int(x) for x in input().split()]
	ele = n // 2
	if n == 1:
		print(0)
	elif n == 2:
		print(s)
	else:
		print(2 * s)


from sys import stdin
tc = int(stdin.readline())
for _ in range(tc):
	(n, m) = list(map(int, stdin.readline().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


x = int(input())
for i in range(x):
	nm = list(map(int, input().split()))
	n = nm[0]
	m = nm[1]

	def array(n, m):
		if n == 1:
			return 0
		elif n == 2:
			return m
		else:
			return 2 * m
	print(array(n, m))


from sys import *
from collections import *
from math import *
input = stdin.readline
tc = int(input())
for tcase in range(tc):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	n = (n + 1) // 2
	if n is 1:
		print(m)
		continue
	numbers = m // n
	lastNumber = m - numbers * (n - 1)
	ans = numbers * 2 * (n - 1)
	ans += lastNumber * 2
	print(ans)


import math
import bisect
T = int(input())
for t in range(T):
	(N, M) = map(int, input().split())
	if N == 1:
		print(0)
	elif N == 2:
		print(M)
	else:
		print(M * 2)


from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [i for i in input().split()]
t = int(input())
while t:
	t -= 1
	(n, k) = value()
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


from math import ceil
for _ in range(int(input())):
	(n, s) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(s)
	if n > 2:
		print(2 * s)


def solve(n, m):
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)

def main():
	t = int(input())
	for i in range(t):
		mn = input().split()
		n = int(mn[0])
		m = int(mn[1])
		solve(n, m)
main()


def solve_task(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m

def main():
	n_tests = int(input())
	solutions = []
	for t in range(n_tests):
		aa = input().split()
		solutions.append(solve_task(int(aa[0]), int(aa[1])))
	for s in solutions:
		print(s)
main()


t = int(input())
for i in range(t):
	(n1, m1) = input().split(' ')
	n = int(n1)
	m = int(m1)
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return m * 2

def main():
	T = int(input())
	for _ in range(T):
		(n, m) = map(int, input().split())
		print(solve(n, m))
main()


import math
for w in range(int(input())):
	sc = list(map(int, input().split()))
	if sc[0] < 2:
		print(0)
	else:
		print(sc[1] * 2 if sc[0] > 2 else sc[1])


import sys, math, random
input = sys.stdin.readline

def inp():
	return int(input())

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

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

def invr():
	return map(int, input().split())
t = inp()
for _ in range(t):
	(k, n) = invr()
	if k == 1:
		print(0)
	elif k == 2:
		print(n)
	else:
		print(2 * n)


t = int(input())
results = []
for testcase in range(t):
	(n, m) = map(int, input().split())
	results.append(min(2, n - 1) * m)
for i in results:
	print(i)


answer = []
for i in range(int(input())):
	count = 0
	(n, m) = list(map(int, input().split()))
	if n == 1:
		count = 0
	elif n == 2:
		count = m
	else:
		count = m * 2
	answer.append(count)
for i in range(len(answer)):
	if answer[i] == -1:
		print(-1)
	else:
		print(answer[i])


def max_calculator(no_of_value, sum):
	if no_of_value == 1:
		return 0
	elif no_of_value == 2:
		return sum
	else:
		return 2 * sum
t = int(input())
lst = list()
for test_case in range(t):
	lst.append(input())
for values in lst:
	values = values.split()
	values = list(map(int, values))
	print(max_calculator(values[0], values[1]))


test_case = int(input())
while test_case:
	(n, m) = map(int, input().split())
	print(min(2, n - 1) * m)
	test_case -= 1


max_int = 1000000001
min_int = -1000000001
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(m * 2)


for tt in range(int(input())):
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


num = int(input())
for i in range(num):
	arr = input()
	arr = arr.split(' ')
	lenz = int(arr[0])
	sum = int(arr[1])
	if lenz == 1:
		print(0)
	elif lenz < 3:
		print(sum)
	else:
		print(sum * 2)


for _ in range(int(input())):
	(n, b) = map(int, input().split())
	a = min(2, n - 1)
	print(a * b)


from atexit import register
from io import StringIO
import sys
_IB = sys.stdin.read().splitlines()
input = iter(_IB).__next__
_OB = StringIO()
sys.stdout = _OB

@register
def write():
	sys.__stdout__.write(_OB.getvalue())

def inpint():
	return int(input())

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

def main():
	t = inpint()
	for _ in range(t):
		(n, m) = inpimap()
		if n > 2:
			print(2 * m)
		elif n == 2:
			print(m)
		else:
			print(0)
main()


n = int(input())
while n > 0:
	(p, q) = input().split(' ')
	m1 = int(p)
	n1 = int(q)
	if m1 > 2:
		print(2 * n1)
	elif m1 == 2:
		print(n1)
	elif m1 == 1 or m1 == 0:
		print('0')
	n = n - 1


def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
t = int(input())
for j in range(t):
	(n, k) = map(int, input().split())
	print(solve(n, k))


import math
import sys
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n >= 3:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


import sys
import math

def main():
	input_list = get_input()
	for line in input_list[1:]:
		print(func(line.split(' ')[0], line.split(' ')[1]))

def get_input():
	input_list = []
	for line in sys.stdin:
		input_list.append(line.rstrip('\n'))
	return input_list

def func(n, m):
	n = int(n)
	m = int(m)
	if n == 1:
		return 0
	elif n == 2:
		return m
	elif n > 2:
		return 2 * m
main()


import sys
from math import ceil
TEST = 0
if TEST == 1:
	input_file = open('inputA.txt', 'r')
	input_from_file = input_file.readlines()

def read_int():
	if TEST:
		return int(input_from_file.pop(0))
	else:
		return int(sys.stdin.readline())

def read_ints():
	if TEST:
		return list(map(int, input_from_file.pop(0).rstrip().split()))
	else:
		return list(map(int, sys.stdin.readline().split()))
t = read_int()
for j in range(t):
	(n, m) = read_ints()
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	print(ans)


tc = int(input())
for ctr in range(tc):
	(n, m) = map(int, input().split())
	print(min(2, n - 1) * m)


for i in range(int(input())):
	(a, b) = [int(x) for x in input().split()]
	if a == 1:
		print('0')
	if a == 2:
		print(b)
	if a >= 3:
		print(2 * b)


from sys import stdin, exit
input = stdin.readline

def i():
	return input()

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

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

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

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	(n, m) = iis()
	if n > 2:
		print(m * 2)
	elif n == 2:
		print(m)
	else:
		print(0)


for i in range(int(input())):
	(n, m) = (int(j) for j in input().split())
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	if n < 3:
		if n == 2:
			print(k)
		else:
			print(0)
	else:
		print(k * 2)


def MostUnstableArray(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
Lista = []
total = int(input())
for i in range(total):
	All = [int(i) for i in input().split()]
	Lista.append(All)
for j in Lista:
	print(MostUnstableArray(j[0], j[1]))


import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
t = int(input())
for case in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


a = int(input())
for x in range(a):
	(b, c) = map(int, input().split())
	if b % 2 == 0:
		if b == 2:
			print(c)
		else:
			print(c * 2)
	elif b == 1:
		print(0)
	else:
		print(c * 2)


import math
import string
import random
from random import randrange
from collections import deque
from collections import defaultdict
t = int(input())
while t > 0:
	t -= 1
	(n, m) = map(int, input().strip().split(' '))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import sys, bisect
from math import gcd
input = sys.stdin.readline

def inp():
	return int(input().rstrip())

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

def insr():
	s = input().rstrip()
	return s[:len(s) - 1]

def invr():
	return map(int, input().rstrip().split())
t = inp()
for _ in range(t):
	(n, m) = invr()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
mod = 1000000007

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

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

def I():
	return int(stdin.readline())

def printIn(ob):
	return stdout.write(str(ob))

def powerLL(n, p):
	result = 1
	while p:
		if p & 1:
			result = result * n % mod
		p = int(p / 2)
		n = n * n % mod
	return result

def myCode():
	(n, m) = In()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)

def main():
	for t in range(I()):
		myCode()
main()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = m // 2
	b = m // 2 + (m & 1)
	if n == 1:
		print(0)
	else:
		factor1 = min([2, n - 1])
		factor2 = min([2, n - 2])
		print(max([factor1 * m, factor2 * (a + b)]))


import math as mt
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
t = I()
for _ in range(t):
	(a, b) = M()
	if a == 1:
		print(0)
	elif a <= 2:
		print(b)
	else:
		print(2 * b)


num_lines = int(input())
lines = []
for i in range(num_lines):
	lines.append([int(i) for i in input().split()])
for line in lines:
	if line[0] == 1:
		print(0)
	elif line[0] == 2:
		print(line[1])
	else:
		print(line[1] * 2)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	k = 0
	if x == 1:
		print(k)
		continue
	elif x == 2:
		print(y)
		continue
	else:
		print(2 * y)


import sys
import math
from collections import defaultdict
mod = 1000000007
T = 1
T = int(sys.stdin.readline())
for _ in range(T):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n == 3:
		print(m << 1)
	else:
		if n & 1 == 0:
			n -= 1
		x = n >> 1
		y = m // x
		a = 0
		if 1:
			a = (n - 3) * y
			z = m - (x - 1) * y
			a += z << 1
		print(a)


import math
import functools

def s():
	(n, m) = list(map(int, input().split(' ')))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)
t = int(input())
for i in range(t):
	s()


def sol(n, m):
	if n == 0:
		return 0
	elif n == 1:
		return 0
	elif n == 2:
		return m
	elif n >= 3:
		return 2 * m
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	v = sol(n, m)
	print(v)


num_cases = int(input())
for _ in range(num_cases):
	inputs = list(map(int, input().split(' ')))
	(n, m) = (inputs[0], inputs[1])
	if n == 1:
		print('0')
	elif n == 2:
		print(m)
	else:
		print(2 * m)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	(n, m) = zz()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import math
import collections
from sys import stdin, stdout
import bisect as bs
T = int(stdin.readline())
for _ in range(T):
	(n, m) = list(map(int, stdin.readline().split()))
	if n >= 3:
		print(2 * m)
		continue
	if n == 2:
		print(m)
		continue
	else:
		print(0)


t = int(input())
for i in range(t):
	num1 = list(map(int, input().split()))
	n = num1[0]
	m = num1[1]
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


for s in range(int(input())):
	a = list(map(int, input().split()))
	if a[0] == 1:
		print(0)
	else:
		print((2 * a[1], a[1])[a[0] == 2])


for _ in range(int(input())):
	(m, n) = map(int, input().split())
	if m == 1:
		print(0)
		continue
	if m == 2:
		print(n)
		continue
	ec = n // (m // 2)
	rem = n % (m // 2)
	res = rem * 2
	res += ec * 2 * (m // 2)
	print(res)


from sys import stdin
import math
a = int(stdin.readline())
for b in range(0, a):
	c = stdin.readline().split()
	A = int(c[0])
	B = int(c[1])
	if A == 1:
		print(0)
		continue
	if A == 2:
		print(B)
		continue
	else:
		print(2 * B)


import math
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		f = math.floor(m / 2)
		c = math.ceil(m / 2)
		print(f * 2 + c * 2)


from sys import stdin, stdout
for testcases in range(int(stdin.readline())):
	(n, m) = map(int, stdin.readline().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		stdout.write(str(2 * m) + '\n')


a = int(input())
for fewwf in range(a):
	lst = input().split()
	lst = [int(i) for i in lst]
	if lst[0] == 1:
		print('0')
	elif lst[0] == 2:
		print(lst[1])
	else:
		print(2 * lst[1])


t = int(input())
answer = []
for i in range(t):
	n = list(map(int, input().split(' ')))
	if n[0] == 1:
		answer.append(0)
	elif n[0] == 2:
		answer.append(n[1])
	else:
		answer.append(n[1] * 2)
for i in range(t):
	print(answer[i])


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
tc = ii1()
for _ in range(tc):
	(n, m) = iia()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


t = int(input())
for _ in range(t):
	(length, summ) = [int(x) for x in input().split()]
	if length == 1:
		print(0)
		continue
	if length % 2 == 1:
		print(2 * summ)
	elif length == 2:
		print(summ)
	else:
		print(2 * summ)


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

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

def solve():
	(n, m) = rl()
	if n == 1:
		print(0)
		return
	elif n == 2:
		print(m)
		return
	else:
		print(2 * m)
		return
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


def cal(arr):
	n = arr[0]
	m = arr[1]
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m

def web():
	T = int(input())
	for t in range(1, T + 1):
		arr = [int(s) for s in input().split(' ')]
		ans = cal(arr)
		print(str(ans))

def file():
	x = open('p1_in.txt')
	T = int(x.readline())
	for t in range(1, T + 1):
		arr = [int(s) for s in x.readline().split(' ')]
		ans = cal(arr)
		print(str(ans))
web()


def main():
	t = int(input())
	for c in range(t):
		(n, m) = [int(s) for s in input().split(' ')]
		print(solve(n, m))

def solve(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
main()


def solution(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	num = solution(n, m)
	print(num)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

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

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

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		(n, m) = In()
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
main()


t = int(input())
answer = []
for a in range(t):
	line = input().split()
	n = int(line[0])
	m = int(line[1])
	if n == 0:
		answer.append(0)
	else:
		answer.append(min(2, n - 1) * m)
for b in answer:
	print(str(b))


from sys import stdin, stdout
import math
n = int(stdin.readline())
for i in range(n):
	arr = [int(x) for x in stdin.readline().split()]
	ans = 0
	if arr[0] == 1:
		ans = 0
	elif arr[0] == 2:
		ans = arr[1]
	else:
		ans = 2 * arr[1]
	stdout.write(str(ans) + '\n')


for case in range(int(input())):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	if n >= 3:
		print(2 * m)
	if n == 1:
		print(0)
	if n == 2:
		print(m)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n != 1 and n != 2:
		print(2 * m)


I = lambda : list(map(int, input().split()))
t = I()[0]
while t:
	t -= 1
	(n, m) = I()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for case in range(int(input())):
	(n, m) = [int(a) for a in input().split()]
	print(min(2, n - 1) * m)


t = int(input())
l = []
for i in range(0, t):
	m = [int(x) for x in input().split()]
	l.append(m)
for j in range(0, t):
	p = l[j][0]
	q = l[j][1]
	if p == 1:
		print(0)
	elif p == 2:
		print(q)
	else:
		print(q * 2)


size = int(input())
(n, m) = ([], [])
i = 0
while i < size:
	tmp = input().split(' ')
	n.append(int(tmp[0]))
	m.append(int(tmp[1]))
	i += 1
i = 0
while i < size:
	if n[i] == 1:
		print(0)
	if n[i] == 2:
		print(m[i])
	if n[i] > 2:
		print(2 * m[i])
	i += 1


def rn():
	a = int(input())
	return a

def rl():
	a = list(map(int, input().split()))
	return a
for _ in range(int(input())):
	[n, m] = rl()
	ans = 0
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	print(ans)


def most_unstable():
	cases = int(input())
	numbers = [list(map(int, input().split())) for i in range(cases)]
	for i in range(len(numbers)):
		if numbers[i][0] == 1:
			print(0)
			continue
		elif numbers[i][0] == 2:
			print(numbers[i][1])
			continue
		print(int(numbers[i][1] * 2))
most_unstable()


for j in range(int(input())):
	(n, m) = tuple(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


l = list()
for i in range(int(input())):
	l.append(map(int, input().split()))
for (n, m) in l:
	print(min(2, n - 1) * m)


from math import *
from collections import *
from operator import itemgetter
import bisect
i = lambda : input()
ii = lambda : int(input())
iia = lambda : list(map(int, input().split()))
isa = lambda : list(input().split())
I = lambda : list(map(int, input().split()))
chrIdx = lambda x: ord(x) - 96
idxChr = lambda x: chr(96 + x)
t = ii()
for _ in range(t):
	(n, m) = iia()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		a = min(m, n // 2)
		v = m // a
		r = 0
		if m % a > 0:
			r = m % (v * a)
		print((v + r) * 2 + v * 2 * (a - 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')
from math import factorial
from collections import Counter, defaultdict
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, m) = RL()
		if n < 2:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
main()


import sys
tests = int(sys.stdin.readline().split()[0])
for test in range(tests):
	numbers = [int(i) for i in sys.stdin.readline().split()]
	(n, m) = (numbers[0], numbers[1])
	if n == 1:
		print(0)
		continue
	if n == 2:
		print(m)
		continue
	print(2 * m)


def solve():
	a = list(map(int, input().split()))
	n = a[0]
	m = a[1]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
t = int(input())
for tt in range(0, t):
	solve()


import sys
from math import ceil, sqrt
read = sys.stdin.buffer.readline
mi = lambda : map(int, read().split())
li = lambda : list(mi())
cin = lambda : int(read())

def isp(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
primes = []
for _ in range(cin()):
	(n, m) = mi()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


ans = []
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ans += [min(2, n - 1) * m]
print('\n'.join(map(str, ans)))


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
tc = int(input())
for zzz in range(tc):
	(n, k) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


t = int(input())
for test in range(t):
	arr = input().split()
	(n, k) = (int(arr[0]), int(arr[1]))
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


tests = int(input())
for i in range(tests):
	numbers = input().split(' ')
	n = int(numbers[0])
	m = int(numbers[1])
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import sys

def main(n, k):
	if n <= 1:
		return 0
	elif n == 2:
		return k
	else:
		return 2 * k
t = sys.stdin.readline()
for i in range(int(t)):
	(n, k) = map(int, list(sys.stdin.readline().split(' ')))
	print(main(n, k))


t = int(input())
for i in range(t):
	NM = list(map(int, input().split()))
	n = NM[0]
	m = NM[1]
	print(min(2, n - 1) * m)


for i in range(int(input())):
	(n, m) = map(int, input().split())
	ans = {1: 0, 2: m}
	try:
		print(ans[n])
	except:
		print(2 * m)


import sys
input = lambda : sys.stdin.readline().strip()
for i in range(int(input())):
	(a, b) = map(int, input().split())
	if a == 1:
		print(0)
	else:
		print(min(2, a - 1) * b)


n = int(input())
for i in range(n):
	(l, sum) = map(int, input().split())
	if l == 1:
		print('0')
	elif l == 2:
		print(sum)
	else:
		print(sum * 2)


tests = int(input())
for _ in range(tests):
	(n, m) = [int(s) for s in input().split()]
	if n == 1:
		print(0)
		continue
	if n >= 3:
		print(2 * m)
	else:
		print(m)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	f = n // 2
	x = m // f
	rem = m % f
	ans = rem * 2 * (x + 1)
	y = f - rem
	if y > 1:
		ans += x * 2 * (y - 1)
	jj = 2
	if n == 2:
		jj = 1
	ans += x * jj
	print(ans)


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	(n, m) = mints()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
for i in range(mint()):
	solve()


ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
for _ in range(ii()):
	(n, m) = f()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for z in range(t):
	(n, m) = map(int, input().split())
	check = 0
	if n == 1:
		check = 1
	elif n == 2:
		check = 2
	elif n % 2 == 0:
		n = int(n / 2)
	else:
		n = int(n / 2) + 1
	if check == 0:
		per_cell = int(m / n)
		residue = m % n
		answer = n * (2 * per_cell) + 2 * residue
	elif check == 1:
		answer = 0
	elif check == 2:
		answer = m
	print(answer)


def solution(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return m * 2

def main():
	for test in range(int(input())):
		(n, m) = map(int, input().split())
		print(solution(n, m))
main()


t = int(input())

def unstable_array(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return 2 * m
for _ in range(t):
	(n, m) = [int(i) for i in input().split()]
	ans = unstable_array(n, m)
	print(ans)


t = int(input())
for i in range(t):
	(a, b) = [int(tmp) for tmp in input().split()]
	if a == 1:
		print('0')
	elif a == 2:
		print(b)
	elif a > 2:
		print(2 * b)


t = int(input())
for i in range(0, t):
	(n, m) = input().split()
	if int(n) == 1:
		print(0)
	elif int(n) == 2:
		print(int(m))
	elif int(n) > 2:
		print(int(m) * 2)
	else:
		print('Input Error')


for _ in range(int(input())):
	(length, total) = map(int, input().split())
	if length == 1:
		print(0)
	elif length == 2:
		print(total)
	else:
		print(2 * total)


from sys import stdin, exit, setrecursionlimit
from collections import deque
setrecursionlimit(10000000)
input = stdin.readline
lmi = lambda : list(map(int, input().split()))
mi = lambda : map(int, input().split())
si = lambda : input().strip('\n')
ssi = lambda : input().strip('\n').split()
for _ in range(int(input())):
	(n, m) = mi()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def main():
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n >= 3:
		print(m * 2)
	return
t = int(input())
while t:
	main()
	t -= 1


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	(n, m) = li()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
for _ in range(I()):
	solve()


import sys
import math as mt
t = int(input())
for __ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def solve(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	else:
		return m * 2

def main():
	t = int(input())
	for q_itr in range(t):
		(n, m) = input().split(' ')
		result = solve(int(n), int(m))
		print(result)
main()


from math import gcd, ceil
from collections import defaultdict as dd

def lcm(a, b):
	return a * b // gcd(a, b)

def inin():
	return int(input())

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

def ar(element, size):
	return [element for i in range(size)]

def digitsum(num):
	su = 0
	while num:
		su += num % 10
		num //= 10
	return su

def pred(n, k, pointer):
	return pointer - k >= n

def yes():
	print('YES')

def no():
	print('NO')
Testcase = inin()
for _ in range(Testcase):
	(n, m) = inar()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


def nod(a, b):
	while a != b:
		(a, b) = (max(a, b), min(a, b))
		if a % b == 0:
			a -= (a // b - 1) * b
		else:
			a -= a // b * b
	return a
for t in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import math
for ad in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if n > 2:
		k = m // (n // 2)
		y = math.ceil((n - 3) / 2)
		x = y * 2 * k
		x += 2 * (m - k * y)
		print(x)
	elif n == 1:
		print(0)
	elif n == 2:
		print(m)


t = int(input())
for i in range(t):
	(e1, e2) = input().split()
	n = int(e1)
	m = int(e2)
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


test_case = int(input())
for _ in range(test_case):
	(n, sum) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(sum)
	else:
		print(2 * sum)


rr = lambda : input().strip()
rrm = lambda : map(int, rr().split())

def solve(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
T = int(rr())
for _ in range(T):
	(n, m) = rrm()
	ans = solve(n, m)
	print(ans)


test_cases = int(input())
for _ in range(test_cases):
	(n, m) = [int(i) for i in input().split(' ')]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	if n <= 1:
		print('0')
	else:
		x = m / 2
		if n == 2:
			print(m)
		elif n == 3 or n == 4:
			print(m * 2)
		elif n == 5:
			z = int(x * 4)
			print(z)
		else:
			z = int(x * 4)
			print(z)


a = int(input())
for i in range(a):
	c = [int(j) for j in input().split(' ')[:2]]
	if c[0] == 1:
		print(0)
	elif c[0] == 2:
		print(c[1])
	else:
		print(2 * c[1])


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())
t = int(input())
for T in range(t):
	(n, x) = get_ints()
	if n == 1:
		print(0)
	elif n == 2:
		print(x)
	else:
		print(x * 2)


t = int(input())
while t:
	(n, m) = map(int, input().split())
	if n % 2 == 0:
		f = 1
	else:
		f = 0
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		n = n // 2
		rem = m % n
		k = m // n
		ans = rem * 2 + k * n * 2
		print(ans)
	t -= 1


for _ in range(int(input())):
	(a, m) = list(map(int, input().split()))
	if a == 1:
		print(0)
	elif a == 2:
		print(m)
	else:
		print(2 * m)


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

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

def lsplit():
	return list(map(int, list(input())))
t = int(input())
for _ in range(t):
	(n, m) = mput()
	if n > 2:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


from sys import stdin

def solve(n, m):
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
	return

def main():
	inp = stdin.readline
	for _ in range(int(inp())):
		(n, m) = map(int, inp().split())
		solve(n, m)
main()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	x = lambda a: 2 * m if a > 2 else m * (n // 2)
	print(x(n))


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(N, M) = [int(x) for x in input().split()]
		if N == 1:
			print(0)
		elif N == 2:
			print(M)
		else:
			print(M * 2)
main()


t = int(input())
for q in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(m)
		continue
	elif n > 2:
		print(m * 2)
		continue


for _ in range(int(input())):
	(j, k) = map(int, input().split())
	if j > 2:
		print(2 * k)
	elif j == 2:
		print(k)
	else:
		print('0')


from sys import stdin
t = int(stdin.readline())
while t > 0:
	(n, k) = [int(x) for x in stdin.readline().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)
	t -= 1


tcase = int(input().strip())
for case in range(tcase):
	(n, m) = map(int, input().strip().split())
	if n == 1:
		ans = 0
	elif n == 2:
		ans = m
	else:
		ans = 2 * m
	tcase = tcase - 1
	print(ans)


import sys
input = sys.stdin.readline
import heapq
import copy
import math

def inp():
	return int(input())

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

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

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

class Node:

	def _init_(self, val):
		self.data = val
		self.left = None
		self.right = None

def bin_search(arr, l, r, val):
	if arr[r] <= val:
		return r + 1
	if r - l < 2:
		if arr[l] > val:
			return l
		else:
			return r
	mid = int((l + r) / 2)
	if arr[mid] <= val:
		return bin_search(arr, mid, r, val)
	else:
		return bin_search(arr, l, mid, val)

def pr_list(a):
	print(*a, sep=' ')

def find_permut(hashm, l):
	ans = 1
	total = math.factorial(l)
	for i in hashm:
		if i != 0:
			ans = ans * math.factorial(i)
	return int(total / ans)

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	for test in range(tests):
		[n, m] = inlt()
		if n > 2:
			print(2 * m)
		elif n == 1:
			print(0)
		else:
			print(m)
main()


a = int(input())
for i in range(0, a):
	(n, m) = map(int, input().split())
	if n >= 4:
		print(2 * m)
	elif n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n == 3:
		print(2 * m)


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print('0')
	else:
		print(min(2, n - 1) * m)


TEST = int(input())
answers = []
for _ in range(TEST):
	(size, total) = [int(x) for x in input().split()]
	if size == 1:
		answers.append(0)
		continue
	ans = total * (2 if size > 2 else 1)
	answers.append(ans)
for ans in answers:
	print(ans)


def single_integer():
	return int(input())

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

def string():
	return input()

def multi_string():
	return input().split()
n = single_integer()
for i in range(n):
	(n, m) = multi_integer()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def solution():
	(n, m) = map(lambda x: int(x), input().split())
	if n == 1:
		print(0)
		return
	if n == 2:
		print(m)
		return
	print(m * 2)
t = int(input())
for _ in range(t):
	solution()


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

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 swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

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
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]
spf_sieve()

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

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

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(n, m) = int_array()
	if n > 2:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


def solve(n, k):
	if n == 1:
		return 0
	if n == 2:
		return k
	else:
		return k * 2
testCases = int(input())
while testCases:
	(N, K) = map(int, input().split())
	print(solve(N, K))
	testCases -= 1


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, M):
	if N == 1:
		return 0
	if N == 2:
		return M
	return 2 * M
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N, M) = [int(x) for x in input().split()]
	ans = solve(N, M)
	print(ans)


def solve_test_case(t):
	(n, m) = parse_array(int)
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)

def parse_array(f):
	return list(map(f, input().split()))

def solve():
	T = int(input())
	for t in range(T):
		solve_test_case(t)
solve()


from sys import stdin, stdout
import math
from collections import defaultdict
input = stdin.buffer.readline
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def main():
	T = int(input())
	for i in range(1, T + 1):
		(n, m) = [int(k) for k in input().split(' ')]
		if n == 1:
			print('0')
			continue
		if n == 2:
			print(int(m))
			continue
		print(int(2 * m))
main()


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for t in range(test):
		l = [int(i) for i in input().split(' ')]
		n = l[0]
		m = l[1]
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(2 * m)
main()


import sys
input = sys.stdin.readline

def solve(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
t = int(input())
for _ in range(t):
	[n, m] = list(map(int, input().split()))
	print(solve(n, m))


t = int(input())
while t > 0:
	(n, k) = map(int, input().strip().split())
	if n == 1:
		print('0')
	if n == 2:
		print(k)
	if n > 2:
		print(k * 2)
	t = t - 1


def createArray(n, m):
	if n == 1:
		print('0')
	elif n == 2:
		print(m)
	else:
		print(m * 2)
t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	createArray(n, m)


t = int(input())
for i in range(t):
	nums = list(map(lambda x: int(x), input().split()))
	(n, k) = (nums[0], nums[1])
	if n == 1:
		print(0)
	elif n == 2:
		print(k)
	else:
		print(2 * k)


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
import string

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

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

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

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

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

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' ', end='\n'):
	print(sep.join(map(str, a)), end=end)

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
for _ in range(ii()):
	(n, m) = mi()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import math
t = int(input())
for u in range(t):
	(n, m) = list(map(int, input().split()))
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	elif n >= 3:
		print(2 * m)


from math import *
from collections import *
from itertools import *
from bisect import *
from operator import *
import sys
sys.setrecursionlimit(1000000)

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

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

def o():
	return int(input())

def kl(con, x=0):
	if x == 0:
		print('Yes') if con else print('No')
	elif x == 1:
		print('yes') if con else print('no')
	elif x == 2:
		print('YES') if con else print('NO')
p = input
for _ in range(o()):
	(a, b) = io()
	if a == 1:
		print(0)
	elif a == 2:
		print(b)
	else:
		print(2 * b)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	spots = n // 2
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n == 3:
		print(2 * m)
	elif n % 2 == 0:
		a = m // spots
		p = a * (spots - 1)
		print(2 * p + 2 * (m - p))
	else:
		a = m // spots + 1
		p = a * (spots - 1)
		print(2 * p + 2 * (m - p))


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

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

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

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

def solve(n):
	if n[0] == 1:
		return 0
	elif n[0] == 2:
		return n[1]
	else:
		return 2 * n[1]

def main():
	test_num = input()
	for _ in range(int(test_num)):
		n = inlt()
		answer = solve(n)
		print(answer)
main()


from math import ceil
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	else:
		print(min(2, n - 1) * m)


n = int(input())
i = 0
while i < n:
	(x, y) = map(int, input().split())
	if x == 1:
		print(0)
	elif x == 2:
		print(y)
	else:
		print(2 * y)
	i = i + 1


t = int(input())
for q in range(t):
	(n, m) = [int(i) for i in input().split(' ')]
	if n == 1:
		answ = 0
	elif n % 2 == 1:
		answ = 2 * m
	elif n == 2:
		answ = m
	else:
		answ = 2 * m
	print(answ)


import sys
sys.setrecursionlimit(10 ** 6)

def f(n, m):
	if 1 == n:
		return 0
	if 2 == n:
		return m
	return m + m
nofkase = int(input())
for kase in range(nofkase):
	(n, m) = [int(i) for i in input().split()]
	print(f(n, m))


import sys
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
from math import ceil
t = [int(val) for val in sys.stdin.readline().split()][0]
for test in range(t):
	(n, m) = [int(val) for val in sys.stdin.readline().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import collections as cc
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n, m) = I()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif n > 2:
		print(2 * m)


t = int(input())
final = []
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		final.append(0)
	elif n == 2:
		final.append(m)
	else:
		final.append(2 * m)
for z in range(t):
	print(final[z])


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	elif m % 2 == 0:
		z = m // 2
		print(2 * m)
	else:
		z1 = m // 2
		z2 = m // 2 + 1
		print(2 * (z1 + z2))


t = int(input())
while True:
	(n, m) = map(int, input().split())
	if n < 2:
		s = 0
	elif n == 2:
		s = m
	elif n > 2:
		s = 2 * m
	print(s)
	t = t - 1
	if t == 0:
		break


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
from random import randint

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

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

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

def main():
	(n, m) = get_tuple()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


case = int(input())
for i in range(case):
	(n, m) = (int(v) for v in input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


def f(a, b):
	if a == 1:
		return 0
	if a == 2:
		return b
	else:
		return 2 * b
t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	(n, m) = [int(i) for i in input().split()]
	res = f(n, m)
	print(res)


def solve(n, m):
	if n == 1:
		res = 0
	elif n == 2:
		res = m
	else:
		res = m * 2
	print(res)
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	solve(a, b)


import sys
import heapq
import math
import bisect

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

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

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

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

def main():
	(n, k) = rinput()
	print((n >= 3) * 2 * k + (n == 2) * k)
for sdfghjkl in range(iinput()):
	main()


from sys import stdout
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for t in range(int(input())):
	(n, m) = map(int, input().split())
	stdout.write(str(min(2, n - 1) * m) + '\n')


import sys
T = int(sys.stdin.readline())
for i in range(T):
	(N, M) = map(int, sys.stdin.readline().split())
	if N == 1:
		print(0)
	elif N == 2:
		print(M)
	elif N >= 3:
		print(M * 2)


try:
	t = int(input())
	for i in range(t):
		(n, m) = map(int, input().split())
		if n == 1:
			print(0)
		elif n == 2:
			print(m)
		else:
			print(m * 2)
except EOFError:
	pass


from sys import stdin
input = iter(stdin.readlines()).__next__
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		x = m
		s = 0
	elif n == 2:
		s = m
	elif n % 2 == 0:
		x = m // (n // 2)
		s = (n - 4) * x + 2 * m - 2 * (n // 2 - 2) * x
	else:
		x = m // (n // 2)
		s = x * (n - 3) + 2 * m - 2 * (n // 2 - 1) * x
	print(s)


def solve(n, m):
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
	return
t = int(input().strip())
n = []
for _ in range(t):
	nm = input().split()
	n = int(nm[0])
	m = int(nm[1])
	solve(n, m)


cases = int(input())
for x in range(cases):
	temp = input()
	num = int(temp.split()[0])
	s = int(temp.split()[1])
	if num == 1:
		print(0)
	elif num == 2:
		print(s)
	elif num == s:
		print(s + num)
	else:
		print(s * 2)


import math
for _ in range(int(input())):
	t = [int(x) for x in input().split()]
	(n, m) = (t[0], t[1])
	if n >= 3:
		print(2 * m)
	elif n == 2:
		print(m)
	else:
		print(0)


def inpint():
	return int(input())

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

def main():
	t = inpint()
	for _ in range(t):
		(n, m) = inpimap()
		print(min(2, n - 1) * m)
main()


t = int(input())
while t > 0:
	(x, y) = list(map(int, input().split()))
	if x == 1:
		print(0)
	else:
		ans = min(2, x - 1) * y
		print(ans)
	t -= 1


Testcase = int(input())
for i in range(Testcase):
	Inputs = list(map(int, input().split()))
	ArrLen = Inputs[0]
	TargetSum = Inputs[1]
	ANS = 0
	if ArrLen == 1:
		print(0)
	elif ArrLen == 2:
		print(TargetSum)
	else:
		print(TargetSum * 2)


def calc_val(n, res):
	total = 0
	for i in range(n - 1):
		total += abs(res[i] - res[i])
	return total

def solve(n, m):
	if n == 1:
		return 0
	elif n == 2:
		return m
	elif n == 3:
		return m * (n - 1)
	return 2 * m
	per = 1
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	print(solve(n, m))


t = int(input())
for ts in range(t):
	n_m = input().split(' ')
	if int(n_m[0]) == 1:
		print(0)
	elif int(n_m[0]) == 2:
		print(n_m[1])
	else:
		print(2 * int(n_m[1]))


import math
t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	print(min(2, n - 1) * m)


n = int(input())
r = []
s = []
for i in range(n):
	(rr, ss) = input().split(' ')
	r.append(int(rr))
	s.append(int(ss))
o = []
for i in range(n):
	if r[i] == 1:
		o.append(0)
	elif r[i] == 2:
		o.append(s[i])
	else:
		o.append(2 * s[i])
print('\n'.join([str(i) for i in o]))


import sys
inp = [int(x) for x in sys.stdin.buffer.read().split()]
ii = 0
qqq = inp[ii]
ii += 1
for _ in range(qqq):
	(n, m) = inp[ii:ii + 2]
	ii += 2
	if n is 1:
		print(0)
	elif n is 2:
		print(m)
	else:
		print(2 * m)


def fname(n, m):
	return min(2, n - 1) * m
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	print(fname(n, m))


def solution(a, b):
	if a > 2:
		return 2 * b
	return b if a == 2 else 0
import sys
iput = sys.stdin.readline
for x in range(int(iput())):
	A = list(map(int, iput().split()))
	a = A[0]
	b = A[1]
	print(solution(a, b))


for t in range(int(input())):
	arr = input().split()
	n = int(arr[0])
	m = int(arr[1])
	if n < 3:
		print(m * (n - 1))
	else:
		print(m + m)


def ProcessInputs():
	(N, M) = map(int, input().strip().split())
	K = int(N / 2)
	if N == 2:
		return M
	if N == 1:
		return 0
	if N % 2 == 1:
		return 2 * M
	else:
		return 2 * M
T = int(input())
for _ in range(T):
	print(ProcessInputs())


import sys
input = lambda : sys.stdin.readline()
int_arr = lambda : list(map(int, input().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().split())
get_int = lambda : map(int, input().split())
get_flo = lambda : map(float, input().split())
mod = 1000000007

def solve():
	pass
for _ in range(int(input())):
	(n, m) = get_int()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)


import sys
import math

def inputnum():
	return int(input())

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

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

def inputstring():
	return [x for x in input()]
t = int(input())
for q in range(t):
	(n, m) = inputnums()
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


T = int(input())
for i in range(T):
	(n, m) = map(int, input().split())
	print(0 if n == 1 else m if n == 2 else 2 * m)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, functools

def solve(line):
	(n, m) = map(int, line.split())
	if n == 1:
		return 0
	if n == 2:
		return m
	(nu, r) = (m // (n // 2), m % (n // 2))
	if n % 2 == 0:
		return nu * n + r * 2
	return nu * (n - 1) + r * 2
T = int(sys.stdin.readline())
t = 0
while True:
	line = sys.stdin.readline().rstrip()
	if not line:
		break
	print(solve(line))
	t += 1


t = int(input())
result = []
for _ in range(t):
	(n, m) = [int(usr_input) for usr_input in input().split(' ')]
	if n <= 1:
		result.append(0)
	elif n == 2:
		result.append(m)
	else:
		result.append(m * 2)
for item in result:
	print(item)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1:
		res = 0
	if n == 2:
		res = m
	if n > 2:
		res = 2 * m
	print(res)


for i in range(int(input())):
	(l, s) = map(int, input().split())
	if l == 1:
		print(0)
	elif l == 2:
		print(s)
	else:
		total = l // 2
		count = s / total * (l - 1)
		if l % 2 == 0:
			count = count + s / total
		print(round(count))


kl = int(input())
for l in range(kl):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)


import sys
from math import log, sqrt, factorial

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

def si():
	return input()

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

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())
tt = ii()
while tt:
	tt -= 1
	(n, m) = mi()
	if n == 2:
		print(m)
	elif n == 1:
		print(0)
	else:
		print(2 * m)


from sys import stdin, stdout, exit as sys_ret
(f_input, f_print, f_flush) = (stdin.readline, stdout.write, stdout.flush)
for _ in range(int(f_input())):
	(amount, maxsum) = map(int, f_input().split())
	if amount == 1:
		f_print('0\n')
	elif amount == 2:
		f_print(str(maxsum) + '\n')
	else:
		f_print(str(maxsum * 2) + '\n')


p = int(input())
while p:
	l = list(map(int, input().split()))
	print(min(2, l[0] - 1) * l[1])
	p = p - 1


t = int(input())
while t > 0:
	(n, m) = [int(x) for x in input().strip().split(' ')]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(m * 2)
	t = t - 1


_IS_DEBUG_MODE = False

def Problem():
	if _IS_DEBUG_MODE == True:
		testcaseList = []
		testcaseList.append([1, 100])
		testcaseList.append([2, 2])
		testcaseList.append([5, 5])
		testcaseList.append([2, 1000000000])
		testcaseList.append([1000000000, 1000000000])
	else:
		t = int(input())
		testcaseList = []
		for testcaseIndex in range(t):
			(n, m) = map(int, input().split())
			testcaseList.append([n, m])
	return testcaseList

def Solver(testcaseList):
	for (n, m) in testcaseList:
		if _IS_DEBUG_MODE == True:
			print('testcase', n, m)
		if n == 1:
			res = 0
		elif n == 2:
			res = m
		else:
			res = m * 2
		print(res)
if _IS_DEBUG_MODE == True:
	import time
	startTime = time.time()
testcaseList = Problem()
Solver(testcaseList)
if _IS_DEBUG_MODE == True:
	endTime = time.time()
	print('time', endTime - startTime)


tests = input()
tests = int(tests)
final_lst = list()
for i in range(tests):
	n_and_m = input()
	n = int(n_and_m.split()[0])
	m = int(n_and_m.split()[1])
	if n % 2 == 0:
		if n == 2:
			max_sum = m
		else:
			max_sum = 2 * m
	elif n == 1:
		max_sum = 0
	else:
		max_sum = 2 * m
	final_lst.append(int(max_sum))
for i in final_lst:
	print(i)


import sys
input = sys.stdin.buffer.readline

def main():
	(n, m) = map(int, input().split())
	if n == 1:
		return 0
	elif n == 2:
		return m
	else:
		return 2 * m
t = int(input())
while t:
	t -= 1
	print(main())


tests = int(input())
for _ in range(tests):
	(numbers, sum_num) = map(int, input().split())
	if numbers > 2:
		result = sum_num * 2
	elif numbers == 2:
		result = sum_num
	else:
		result = 0
	print(result)


def solve():
	(n, m) = [int(v) for v in input().split()]
	if n == 1:
		print(0)
	elif n == 2:
		print(m)
	else:
		print(2 * m)
t = int(input())
for _ in range(t):
	solve()


def func(nm):
	(n, m) = list(map(int, nm.split()))
	if n == 1:
		return 0
	if n == 2:
		return m
	return m * 2

def main():
	t = int(input())
	for a in map(func, [input() for i in range(t)]):
		print(a)
main()


for x in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if n > 2:
		print(m * 2)
	elif n == 2:
		print(m)
	else:
		print(0)


t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	result = min(2, n - 1) * m
	print(result)


for ii in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
		continue
	print(min(2, n - 1) * m)


for tc in range(0, int(input())):
	(n, m) = map(int, input().split())
	if n == 1:
		print(0)
	else:
		print(m if n < 3 else m * 2)


W = int(input())
for i in range(W):
	X = input()
	for E in range(len(X)):
		u = X[E]
		if u == ' ':
			n = int(X[0:E])
			m = int(X[E + 1:-1] + X[-1])
	if n == 1:
		print(0)
	if n == 2:
		print(m)
	if n > 2:
		print(2 * m)


def max_abs_diff(n, m):
	if n == 1:
		return 0
	if n == 2:
		return m
	return m * 2

def main():
	for _ in range(int(input().strip())):
		(n, m) = map(int, input().strip().split(' '))
		print(max_abs_diff(n, m))
main()