#include <bits/stdc++.h>
using namespace std;
bool isin(int i, int j) { return ((i & (1 << j)) > 0); }
void solve() {
  int n, m;
  cin >> n >> m;
  if (n == 1) cout << 0 << '\n';
  if (n == 2) cout << m << '\n';
  if (n >= 3) cout << 2 * m << '\n';
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
