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