#include <bits/stdc++.h>
using namespace std;
bool isprime(long long int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (long long int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (long long int i = p * p; i <= n; i += p) {
        cout << i << endl;
        prime[i] = false;
      }
    }
  }
  return prime[n];
}
void code1() {
  int n, m;
  cin >> n >> m;
  if (n == 1) {
    cout << "0" << endl;
  } else if (n == 2) {
    cout << m << endl;
  } else
    cout << 2 * m << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    code1();
  }
  return 0;
}
