#include <bits/stdc++.h>
using namespace std;
int exp(int a, int b) {
  if (b == 0) return 1;
  int ans, temp;
  temp = exp(a, b / 2);
  if (b % 2 == 0)
    ans = temp * temp;
  else
    ans = a * temp * temp;
  return ans;
}
int main() {
  int i, t, n, m, currAns, half, diff;
  cin >> t;
  vector<int> ans(t);
  for (i = 0; i < t; i++) {
    cin >> n >> m;
    if (n == 1)
      currAns = 0;
    else if (n == 2)
      currAns = m;
    else
      currAns = m << 1;
    ans[i] = currAns;
  }
  for (i = 0; i < t; i++) cout << ans[i] << '\n';
  return 0;
}
