#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 228;
const long long mod = (long long)(1e9 + 7);
const int INF = (long long)(1e9 + 7);
const double PI = 3.141592653589793238462643;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    if (n == 1) {
      cout << 0 << "\n";
      continue;
    }
    if (n > 2)
      cout << 2 * m << "\n";
    else
      cout << m << "\n";
  }
  return 0;
}
