#include <bits/stdc++.h>
using namespace std;
void pr(int arr[], int size) {
  for (long long int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
  }
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n, sum;
    cin >> n >> sum;
    if (n == 1) {
      cout << 0 << endl;
    } else if (n == 2) {
      cout << sum << endl;
    } else {
      cout << 2 * sum << endl;
    }
  }
  return 0;
}
