#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, i, s, a, b, g, h, j;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> a >> b;
    if (a == 1) {
      cout << "0" << endl;
      continue;
    }
    if (a == 2) {
      cout << b << endl;
      continue;
    }
    if (b % 2 == 0 && a > 2) {
      g = b / 2;
      g = g + g + g + g;
      cout << g << endl;
      continue;
    } else if (b % 2 != 0 && a > 2) {
      g = b / 2;
      h = (b / 2) + 1;
      g = g + g + h + h;
      cout << g << endl;
      continue;
    }
  }
}
