#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, OO = 0x3f3f3f3f;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int tc, n, m;
int main() {
  startTime = (double)clock();
  scanf("%d", &tc);
  while (tc--) {
    scanf("%d %d", &n, &m);
    if (n < 2)
      puts("0");
    else if (n == 2)
      printf("%d\n", m);
    else
      printf("%d\n", m * 2);
  }
  return 0;
}
