#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t, r, j, s, n, m, n1, n2, n3, k, i, ans;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    if (n == 1)
      s = 0;
    else if (n == 2)
      s = m;
    else
      s = 2 * m;
    cout << s << endl;
  }
}
