#include <bits/stdc++.h>
using namespace std;
long long n, t, m;
int main() {
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld%lld", &n, &m);
    if (n == 1)
      printf("0\n");
    else if (n == 2)
      printf("%lld\n", m);
    else
      printf("%lld\n", m * 2);
  }
  return 0;
}
