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