#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  if (n == 1)
    cout << 0;
  else if (n == 2)
    cout << m;
  else
    cout << 2 * m;
  puts("");
}
int main() {
  int q;
  for (cin >> q; q--;) solve();
  return 0;
}
