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