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