#include <bits/stdc++.h>
using namespace std;
long long a[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    if (n == 1) {
      cout << 0 << endl;
      continue;
    } else if (n == 2) {
      cout << m << endl;
      continue;
    } else {
      cout << m * 2 << endl;
      continue;
    }
  }
  return 0;
}
