#include <bits/stdc++.h>
const double pi = 2.0 * acos(0.0);
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n, m, ans;
    cin >> n >> m;
    if (n == 1) {
      ans = 0;
    } else if (n == 2) {
      ans = m;
    } else {
      ans = m * 2;
    }
    cout << ans << endl;
  }
  return 0;
}
