#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int time;
  cin >> time;
  while (time--) {
    int num, sum;
    cin >> num >> sum;
    if (num == 1) {
      cout << "0" << endl;
    } else if (num == 2) {
      cout << sum << endl;
    } else {
      cout << sum * 2 << endl;
    }
  }
}
