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 ∑_{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 ≤ t ≤ 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 ≤ n, m ≤ 10^9) — the length of the array and its sum correspondingly.

Output

For each test case, print the answer — the maximum possible value of ∑_{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.
import javax.print.attribute.IntegerSyntax;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

	static boolean[] primecheck = new boolean[1000002];
	static ArrayList<Long> lucky = new ArrayList<>();

	public static void main(String[] args) throws IOException {
		OutputStream outputStream = System.out;
		FastReader in = new FastReader();
		PrintWriter out = new PrintWriter(outputStream);
		PROBLEM solver = new PROBLEM();
                int t = in.nextInt();
                while (t-->0){
			solver.solve(in, out);
		}
		out.close();
	}

	public static void luckIsCalling(long n){

		long a = n*10 + 4;
		long b = n*10 + 7;

		lucky.add(a);
		lucky.add(b);

		if(a<=4444444444L) luckIsCalling(a);
		if(b<=4444444444L) luckIsCalling(b);
	}

	static class PROBLEM {

		public void solve(FastReader in, PrintWriter out) {
			  long n = in.nextLong();
			  long m = in.nextLong();
			   out.println(Math.min(2,n-1)*m);

		}
	}

	static boolean isPalindrome(char[] s){
		int flag = 1;
		for (int i = 0; i < s.length; i++) {
			if(s[i] != s[s.length-1-i]){
				flag = 0;
				break;
			}
		}
		if(flag == 1) return true;
		else return false;
	}

	static long gcd(long a, long b) {
		if (b == 0) return a;
		return gcd(b, a % b);
	}

	static boolean isSquare(double a) {
		boolean isSq = false;
		double b = Math.sqrt(a);
		double c = Math.sqrt(a) - Math.floor(b);

		if (c == 0) isSq = true;

		return isSq;
	}

	static int exponentMod(int A, int B, int C) {

		// Base cases
		if (A == 0)
			return 0;
		if (B == 0)
			return 1;

		// If B is even
		long y;
		if (B % 2 == 0) {
			y = exponentMod(A, B / 2, C);
			y = (y * y) % C;
		}

		// If B is odd
		else {
			y = A % C;
			y = (y * exponentMod(A, B - 1,
					C) % C) % C;
		}

		return (int) ((y + C) % C);
	}

	public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
		public U x;
		public V y;

		public Pair(U x, V y) {
			this.x = x;
			this.y = y;
		}

		public int hashCode() {
			return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
		}

		public boolean equals(Object o) {
			if (this == o)
				return true;
			if (o == null || getClass() != o.getClass())
				return false;
			Pair<U, V> p = (Pair<U, V>) o;
			return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
		}

		public int compareTo(Pair<U, V> b) {
			int cmpU = x.compareTo(b.x);
			return cmpU != 0 ? cmpU : y.compareTo(b.y);
		}

		public String toString() {
			return String.format("(%s, %s)", x.toString(), y.toString());
		}

	}

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		char nextChar() {
			return next().charAt(0);
		}

		boolean nextBoolean() {
			return !(nextInt() == 0);
		}
		// boolean nextBoolean(){return Boolean.parseBoolean(next());}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		public int[] readArray(int size) {
			int[] array = new int[size];
			for (int i = 0; i < size; i++) array[i] = nextInt();
			return array;
		}

	}

	private static int[] mergeSort(int[] array) {

		if (array.length <= 1) {
			return array;
		}

		int midpoint = array.length / 2;

		int[] left = new int[midpoint];
		int[] right;

		if (array.length % 2 == 0) {
			right = new int[midpoint];
		} else {
			right = new int[midpoint + 1];
		}

		for (int i = 0; i < left.length; i++) {
			left[i] = array[i];
		}

		for (int i = 0; i < right.length; i++) {
			right[i] = array[i + midpoint];
		}

		int[] result = new int[array.length];

		left = mergeSort(left);
		right = mergeSort(right);

		result = merge(left, right);

		return result;
	}

	private static int[] merge(int[] left, int[] right) {

		int[] result = new int[left.length + right.length];
		int leftPointer = 0, rightPointer = 0, resultPointer = 0;

		while (leftPointer < left.length || rightPointer < right.length) {

			if (leftPointer < left.length && rightPointer < right.length) {

				if (left[leftPointer] < right[rightPointer]) {
					result[resultPointer++] = left[leftPointer++];
				} else {
					result[resultPointer++] = right[rightPointer++];
				}
			} else if (leftPointer < left.length) {
				result[resultPointer++] = left[leftPointer++];
			} else {
				result[resultPointer++] = right[rightPointer++];
			}
		}
		return result;
	}

	public static void Sieve(int n){

		Arrays.fill(primecheck,true);
		primecheck[0]= false;
		primecheck[1] = false;

		for(int i = 2;i*i<n+1;i++){
			if(primecheck[i]){
				for(int j = i*2;j<n+1;j+=i){
					primecheck[j] = false;
				}
			}
		}
	}
}


