#include <bits/stdc++.h>
using namespace std;
long long int n, m;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t, x, y, z, p, q, u, v, ct = 0, flag = 0;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    if (n == 1) {
      cout << 0 << endl;
    } else if (n == 2) {
      cout << m << endl;
    } else {
      cout << 2 * m << endl;
    }
  }
}
