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