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$.

Difficulty: introductory

Starter code:

Solutions:
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)


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


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

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

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)


t = int(input())

for loop in range(t):

    n,m = list(map(int,input().split()))

    if n == 1:
        print(0)
    elif n == 2:
        print (m)
    else:
        print(2*m)


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)









import sys
input = sys.stdin.readline

t=int(input())
for tests in range(t):
    n,m=list(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 = list(map(int,input().split()))
    if (n == 1):
        print(0)
        continue
    if (n==2):
        print(m)
        continue
    print(m*2)

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


def read_ints():
    return list(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 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)

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)

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)


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
import string

#########################
# imgur.com/Pkt7iIf.png #
#########################

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)

########################################################################################################################
# input = sys.stdin.readline

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

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)
    




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

from sys import stdin
# from collections import Counter
# from math import ceil,log

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

# 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
# 	prime[0]=False
# 	prime[1]=False
# 	return prime 

# alpha=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# mod=10**9+7

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)

for i in range(int(input())):
  n,m=[int(i) for i in 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)
    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)
    elif(n==2):
        print(m)
    else:
        print(2*m)