#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    if (n == 1 || n == 0) {
      cout << "0" << endl;
      continue;
    }
    if (n == 2) {
      cout << m << endl;
      continue;
    }
    cout << 2 * m << endl;
  }
  return 0;
}
