#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 200010;
inline void solve() {
  int N = read(), M = read();
  if (N == 1) {
    printf("0\n");
    return;
  }
  if (N == 2) {
    printf("%d\n", M);
    return;
  }
  printf("%d\n", M << 1);
}
int main() {
  int Cas = read();
  while (Cas--) solve();
  return 0;
}
