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