#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x, y, n, tt;
  cin >> tt;
  while (tt--) {
    cin >> x >> y;
    if (x == 1)
      cout << "0" << endl;
    else if (x == 2)
      cout << y << endl;
    else
      cout << y * 2 << endl;
  }
  return 0;
}
