#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    long long sum = 0;
    long long div = n / 2;
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }
    if (n == 2) {
      cout << m << endl;
      continue;
    }
    sum = m * 2;
    cout << sum << endl;
  }
}
