#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  if (n == 1)
    cout << 0 << endl;
  else if (n > 2) {
    cout << m * 2 << endl;
  } else {
    cout << m << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) solve();
  return 0;
}
