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