#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    int ans = min(2, n - 1) * m;
    cout << ans;
    cout << "\n";
  }
  return 0;
}
