#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    int n, k;
    cin >> n >> k;
    if (n == 1) {
      cout << "0" << endl;
    } else if (n == 2) {
      cout << k << endl;
    } else if (n > 2) {
      cout << 2 * k << endl;
    }
  }
}
