#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  scanf("%d%d", &n, &m);
  printf("%lld\n", 1ll * min(n - 1, 2) * m);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
}
