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