#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n, m;
  cin >> n >> m;
  if (n == 1) {
    cout << "0" << endl;
    return;
  }
  if (n == 2) {
    cout << m << endl;
    return;
  }
  if (n > 2) {
    cout << 2 * m << endl;
    return;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
