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.

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.

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

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

var x=parseInt(readline());

while(x--){
  var arr=readline().split(' ').map(x => parseInt(x));
  if(arr[0]<=1)
    print("0");
  else if(arr[0]===2)
    print(arr[1]);
  else
    print(2*arr[1]);

}

var testCaseCount = parseInt(readline());

var currentTestCase = 1;

while(currentTestCase <= testCaseCount) {
    var input = readline().split(" ");
    var n = parseInt(input[0]);
    var m = parseInt(input[1]);

    var result = findResult(n, m);

    print(result);

    currentTestCase += 1;
}

function findResult(n, m) {
    if (n < 2) {
        return 0;
    }

    if (n == 2) {
        return m;
    }

    return 2*m;
}

var t = parseInt(readline());

for (var tc = 0; tc < t; tc++) {
  var nm = readline()
    .split(" ")
    .map((val) => parseInt(val));
  var n = nm[0],
    m = nm[1];
  if (n == 2) print(m);
  else if (n == 1) print(0);
  else print(m * 2);
}

var testCases = readline();
for(var i =0; i< testCases; i++) {
    var line =  readline().split(" ");
    var arrayLength = line[0], arraySum = line[1];
    print( arrayLength >= 3? arraySum*2: (arrayLength == 1? 0 : arraySum));
}

var testCases = readline();

for(var i =0; i< testCases; i++) {
    var line =  readline().split(" ");
    var arrayLength = line[0], arraySum = line[1];
    if(arrayLength >= 3) {
        print(arraySum*2)
    } else {
        if(arrayLength == 1) {
            print(0);
        }
        else {
            print(arraySum);
        }
    }
}

for(var t=Number(readline());t;t--){
 var a = readline().trim().split(/\s/).map(x=>Number(x));
 print((a[0]==1)?0:(a[0]==2)?a[1]:2*a[1]);
}

function solve(n,m) {
    if (n == 1) return 0;
    if (n == 2) return m;
    return 2*m;
}
var tc = Number(readline())
while (tc--) {
  var inputArr = readline().split(" ").map(function(a) { return +a })
  var n = inputArr[0]
  var m = inputArr[1]
  print(solve(n,m))
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', () => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function main() {
    //console.log("inputString", inputString)

    //console.log("typeof inputString", typeof inputString)
    const tests = inputString.slice(1).map(item => item.split(" ").map(Number));

    //console.log("tests", tests);

    let result = [];

    for (let i = 0; i < tests.length; i++) {
        //console.log("tests[i]", tests[i]);
        const [n, m] = tests[i];

        if (n === 1) {
            result.push(0);
        } else if (n === 2) {
            result.push(m);
        } else {
            result.push(m * 2);
        }
    }

    result.forEach(item => console.log(item));
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());
    main();
});

function readline() {
    return inputString[currentLine++];
}

let main = () => {
    let t = parseInt(readline());
    while( t > 0 ){
    	let [n, m] = readline().split(" ").map(Number);
    	if( n === 1 )
    		console.log(0);
    	else if( n ===  2 )
    		console.log(m);
    	else
    		console.log(2*m);
    	t = t-1;
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}
/*****************************************************************/
function main() {
    let t = readLine();
    t = parseInt(t);
    while(t--) {
        let line = readLine();
        line = line.split(" ");
        let n = parseInt(line[0]);
        let m = parseInt(line[1]);
        if(n === 1) {
            console.log("0");
        } else if (n === 2) {
            console.log(m);
        } else {
            console.log(2*m);
        }
    }
}

'use strict';

const parseProblem = (line, problem) => {
    if (!problem.T || problem.T === 0) {
        problem.T = parseInt(line);
        return problem;
    }

    const cases = problem.cases;

    //if (cases.length === 0 || cases[cases.length - 1].arr) {
        cases.push({
            s: line
        });
    /*} else {
        cases[cases.length - 1].arr = line;
    }*/

    const isProcessing = cases.length < problem.T;// || !cases[cases.length - 1].arr;
    problem.isProcessing = isProcessing;

    return problem;
};

const solve = data => {
    const [n, m] = data.s.split(' ').map(i => +i);
    let sum = (n == 1) ? 0 : m;

    if(n > 2) {
        sum = 2 * m;
    }

    return sum;
}

const solveCases = cases => {
    for (let index = 0; index < cases.length; index++) {
        const result = solve(cases[index]);
        console.log(result);
    }
};

const main = () => {
    const readline = require('readline');

    let problem = {
        T: 0,
        cases: [],
        isProcessing: true
    };

    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.on('line', line => {
        problem = parseProblem(line, problem);

        if (!problem.isProcessing) {
            rl.close();
        }
    }).on('close', () => {
        solveCases(problem.cases);
        process.exit(0);
    });
};

if (!module.parent) {
    main();
}

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin
});
rl.on('line', (line) => {
  const l = line.split(' ').map(v => +v);
  if (l.length > 1) {
    if (l[0] === 1) {
      process.stdout.write(`0\n`);
    } else if (l[0] === 2) {
      process.stdout.write(`${l[1]}\n`);
    } else if (l[0] > 2) {
      process.stdout.write(`${l[1] * 2}\n`);
    }
  }
});

var read = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
var obj;
var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  myerr("-----start-----");
  var start = new Date();
  Main();
  var end = new Date() - start;
  myerr("----- end -----");
  myerr("time : " + (end) + "ms");
});
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  var returnObj = {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}}
  };
  return returnObj;
}
function myout(s){console.log(s);}
function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));}
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す 1:数値へ変換
//2:半角SPで分割 4:半角SPで分割し、数値配列へ
//6:1文字で分割  7:1文字で分割し、数値配列へ
//8:半角SPで結合 9:改行で結合 0:文字なしで結合
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map(Number);case 6:return i.split("");case 7:return i.split("").map(Number);case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}}
function Main(){
	var t = nextInt();
	var output = new Array(t);
	for(var i = 0; i < t; i++){
		var one = nextIntArray();
		var N = one[0];
		if(N == 1){
			output[i] = 0;
			continue;
		}
		var M = one[1];
		if(N == 2){
			output[i] = M;
		}else{
			output[i] = M * 2;
		}
	}
	myout(myconv(output,9));
}

// 5
// 1 100
// 2 2
// 5 5
// 2 1000000000
// 1000000000 1000000000

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}

function writeLine(str) {
    process.stdout.write(`${str}\n`);
}

function main() {
    let t = Number(readLine());
    while(t--) {
        const [n, m] = readLine().split(' ').map(num => Number(num));
        if (n == 1) {
            writeLine(0);
        } else if (n == 2) {
            writeLine(m);
        } else {
            writeLine(m*2);
        }
    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => string.trim());

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    const n = parseInt(readline());
    const pairs = [];
    for (let i = 0; i < n; i++) {
        pairs.push(readline().split(' ').map(o => parseInt(o)));
    }

    pairs.forEach(([n, m]) => {
        if (n === 1) {
            console.log(0);
        } else if (n === 2) {
            console.log(m);
        } else {
            console.log(m * 2);
        }
    })
}

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

let c = 0;

rl.on("line", (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const [n, m] = d.split(" ").map(Number);

  console.log(Math.min(2, n - 1) * m);

  // if (n === 1) {
  //   console.log(0);
  //   return;
  // }

  // if (n === 2) {
  //   console.log(m);
  //   return;
  // }

  // console.log(m * 2);
  c++;
});

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

let c = 0;

rl.on("line", (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const [n, m] = d.split(" ").map(Number);

  if (n === 1) {
    console.log(0);
    return;
  }

  if (n === 2) {
    console.log(m);
    return;
  }

  console.log(m * 2);
  c++;
});

const processData = (lines) => {
  const n = +lines[0]
  for (let i=0; i<n; i++) {
    const [n, m] = lines[i+1].split(' ').map(x => +x)
    if (n === 1) {
      console.log(0)
    } else {
      if (n === 2) {
        console.log(m)
      } else {
        console.log(m * 2)
      }
    }
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})

// node template.js < A-small.in > A-small.out

function main() {
    var tests = 1;
    tests = nextInt();

    for (let t = 1; t <= tests; t++) {
        let N = nextInt(), M = nextInt();

        let result = 0;
        if (N>1) result = M;
        if (N>2) result = M*2;

        print(result);
    }
}

// ------- Template ------------------
function newTensor(dimensions, value) {
    let res = [];
    let dim = dimensions[0], subdim = dimensions.slice(1);
    if (subdim.length == 0) {
        for(let i = 0; i < dim; i++) {
            res.push(value);
        }
    } else {
        for(let i = 0; i < dim; i++) {
            res.push(newTensor(subdim, value));
        }
    }
    return res;
}

if (!String.prototype.startsWith) {
    Object.defineProperty(String.prototype, 'startsWith', {
        value: function(search, rawPos) {
            var pos = rawPos > 0 ? rawPos|0 : 0;
            return this.substring(pos, pos + search.length) === search;
        }
    });
}
if (!String.prototype.endsWith) {
    String.prototype.endsWith = function(search, this_len) {
        if (this_len === undefined || this_len > this.length) {
            this_len = this.length;
        }
        return this.substring(this_len - search.length, this_len) === search;
    };
}

var curTokens = [], curToken = 0;

function next() {
    while (curToken >= curTokens.length) {
        curTokens = readline().split(/[\s]+/);
        curToken = 0;
    }
    return curTokens[curToken++];
}

function nextInt() {
    return parseInt(next());
}

// code for nodejs
var inputBuffer = '', curLine = 0;

function readline() {
    return inputBuffer[curLine++];
}

function print(data) {
    process.stdout.write(data + '\n');
}

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function (chunk) {
    inputBuffer += chunk;
});

process.stdin.on('end', function () {
    inputBuffer = inputBuffer.split(/[\s]+/);
    main();
});

// Sample code to perform I/O:

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
    stdin_input += input;                               // Reading input from STDIN
});

process.stdin.on("end", function () {
    main(stdin_input);
});

/**
 *
 * @param {*} str
 * @param {*} seperator
 */
function splitter(str, seperator = '') {
    return str.trim().split(seperator);
}

/**
 *
 * @param {*} arr
 */
function strToNumArr(arr = []) {
    return arr.map(num => Number(num));
}

/**
 *
 * @param {*} num1
 * @param {*} num2
 */
function matrixArray(num1, num2) {
    return Array(num1).fill(Array(num2));
}

let log = function () {
    return Function.prototype.bind.call(console.log, console, '');
}();

function removeEmptyItem(arr, index) {
    return arr.slice(0, index).concat(arr.slice(index + 1, arr.length));
}

function range(min, max) {

    let arr = [];
    for (let index = min; index < max; index++) {
        arr.push(index);

    }
    return arr;
}

function main(input) {
    let output = nameLookUp(input);
    // process.stdout.write(output);       // Writing output to STDOUT
}

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail

// Write your code here
function nameLookUp(str) {

    let inputs = str.split('\n');
    let t = +inputs[0].trim();
    for (let index = 1; index <= t; index++) {
        const element = inputs[index].trim().split(' ');
        let n = +element[0];
        let sum = +element[1];
        if (n > 2) {
            console.log(2 * sum);
        }
        else {
            console.log((n - 1) * sum);
        }
    }

}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    6
1
2
3
4
5
6

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = readline();

    for(let r=0;r<range;r++) {
        let [m,n] = readline().split(" ").map(Number);

        console.log(Math.min(2, (m-1)) * n);
    }
}

function solve(n,m) {
    if (n <= 2) return m;
    return 2*m;
}
var tc = Number(readline())
while (tc--) {
  var inputArr = readline().split(" ").map(function(a) { return +a })
  var n = inputArr[0]
  var m = inputArr[1]
  print(solve(n,m))
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', () => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function main() {
    console.log("inputString", inputString)

    //console.log("typeof inputString", typeof inputString)
    const tests = inputString.slice(1).map(item => item.split(" ").map(Number));

    //console.log("tests", tests);

    let result = [];

    for (let i = 0; i < tests.length; i++) {
        //console.log("tests[i]", tests[i]);
        const [n, m] = tests[i];

        if (n === 1) {
            result.push(0);
        } else if (n === 2) {
            result.push(m);
        } else {
            result.push(m * 2);
        }
    }

    result.forEach(item => console.log(item));
}

process.stdin.resume();
process.stdin.setEncoding("ascii");

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on("data", chunk => input_stdin += chunk.trim());

process.stdin.on("end", () => {
    input_stdin_array = input_stdin.trim().split("\n").map(str => str.trim());
    main()
})

readLine = () => {
    return input_stdin_array[input_currentline++];
}

main = () => {
    let t = parseInt(readLine());
    while( t-- ){
    	let [n, m] = readLine().split(" ").map(x => parseInt(x));
    	if( n === 1 )
    		console.log(0);
    	else if( n ===  2 )
    		console.log(m);
    	else
    		console.log(2*m);
    }
}

process.stdin.resume();
process.stdin.setEncoding("ascii");

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on("data", chunk => input_stdin += chunk.trim());

process.stdin.on("end", () => {
    input_stdin_array = input_stdin.trim().split("\n").map(str => str.trim());
    main()
})

readLine = () => {
    return input_stdin_array[input_currentline++];
}

main = () => {
    let t = parseInt(readLine());
    while( t > 0 ){
    	let [n, m] = readLine().split(" ").map(Number);
    	if( n === 1 )
    		console.log(0);
    	else if( n ===  2 )
    		console.log(m);
    	else
    		console.log(2*m);
    	t = t-1;
    }
}

process.stdin.resume();
process.stdin.setEncoding("ascii");

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on("data", chunk => input_stdin += chunk.trim());

process.stdin.on("end", () => {
    input_stdin_array = input_stdin.trim().split("\n").map(str => str.trim());
    main()
})

readLine = () => {
    return input_stdin_array[input_currentline++];
}

main = () => {
    let t = parseInt(readLine());
    while( t > 0 ){
    	let [n, m] = readLine().split(" ").map(x => parseInt(x));
    	if( n === 1 )
    		console.log(0);
    	else if( n ===  2 )
    		console.log(m);
    	else
    		console.log(2*m);
    	t = t-1;
    }
}

process.stdin.resume();
process.stdin.setEncoding("ascii");

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on("data", chunk => input_stdin += chunk.trim());

process.stdin.on("end", () => {
    input_stdin_array = input_stdin.trim().split("\n").map(str => str.trim());
    main()
})

readLine = () => {
    return input_stdin_array[input_currentline++].trim();
}

main = () => {
    let t = parseInt(readLine());
    while( t > 0 ){
    	let [n, m] = readLine().split(" ").map(x => parseInt(x));
    	if( n === 1 )
    		console.log(0);
    	else if( n ===  2 )
    		console.log(m);
    	else
    		console.log(2*m);
    	t = t-1;
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}
/*****************************************************************/
function main() {
    let t = readLine();
    t = parseInt(t);
    console.log("mai yahan aaya")
    while(t--) {
        let line = readLine();
        line = line.split(" ");
        let n = parseInt(line[0]);
        let m = parseInt(line[1]);
        if(n === 1) {
            console.log("0");
        } else if (n === 2) {
            console.log(m);
        } else {
            console.log(2*m);
        }
    }
}

'use strict';

const parseProblem = (line, problem) => {
    if (!problem.T || problem.T === 0) {
        problem.T = parseInt(line);
        return problem;
    }

    const cases = problem.cases;

    //if (cases.length === 0 || cases[cases.length - 1].arr) {
        cases.push({
            s: line
        });
    /*} else {
        cases[cases.length - 1].arr = line;
    }*/

    const isProcessing = cases.length < problem.T;// || !cases[cases.length - 1].arr;
    problem.isProcessing = isProcessing;

    return problem;
};

const solve = data => {
    const [n, m] = data.s.split(' ').map(i => +i);
    let sum = (n == 1) ? 0 : m;

    if(m == n && n > 2) {
        sum = 2 * m;
    }

    return sum;
}

const solveCases = cases => {
    for (let index = 0; index < cases.length; index++) {
        const result = solve(cases[index]);
        console.log(result);
    }
};

const main = () => {
    const readline = require('readline');

    let problem = {
        T: 0,
        cases: [],
        isProcessing: true
    };

    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.on('line', line => {
        problem = parseProblem(line, problem);

        if (!problem.isProcessing) {
            rl.close();
        }
    }).on('close', () => {
        solveCases(problem.cases);
        process.exit(0);
    });
};

if (!module.parent) {
    main();
}

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin
});
rl.on('line', (line) => {
  const l = line.split(' ').map(v => +v);
  if (l.length > 1) {
    if (l[0] === 1) {
      process.stdout.write(`0\n`);
    } else if (l[0] === 2) {
      process.stdout.write(`${l[1]}\n`);
    } else if (l[0] > 2) {
      process.stdout.write(`${l[1] + l[0]}\n`);
    }
  }
});

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
1 100
2 2
5 5
2 1000000000
1000000000 1000000000

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = readline();

    for(let r=0;r<range;r++) {
        let [x, y] = readline().split(" ").map(Number);

        if(x === 1) {
            console.log(0);
            continue;
        }

        if(x === 2) {
            console.log(y);
            continue;
        }

        //if(x === y) {
        console.log(x + y);
        //}
    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
1 100
2 2
5 5
2 1000000000
1000000000 1000000000

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = readline();

    for(let r=0;r<range;r++) {
        let [x, y] = readline().split(" ").map(Number);

        if(x === 1) {
            console.log(0);
            continue;
        }

        if(x === 2) {
            console.log(y);
            continue;
        }

        if(x === y) {
            console.log(y + y);
            continue;
        } else {
            console.log(y+y+1);
            continue;
        }
    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    6
1
2
3
4
5
6

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let range = readline();

    for(let r=0;r<range;r++) {
        let [m,n] = readline().split(" ").map(Number);

        console.log(Math.min(2, n*(m-1)));
    }
}