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