#include <bits/stdc++.h>
using namespace std;
int solve() {
  int length;
  int sum;
  cin >> length >> sum;
  if (length == 1) {
    return 0;
  } else if (length == 2) {
    return sum;
  } else if (length > 2) {
    return sum * 2;
  }
}
int main() {
  int rez[10000];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    rez[i] = solve();
  }
  for (int i = 0; i < n; i++) {
    cout << rez[i] << endl;
  }
}
