#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using mpi = map<int, int>;
using mpis = map<int, string>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
const int MOD = 1e9 + 7;
const ld PI = 2 * acos(0.0);
template <typename T>
T Swap(T a, T b) {
  T temp = a;
  a = b;
  b = temp;
}
inline void Solve() {
  int n, m;
  cin >> n >> m;
  if (n == 1)
    cout << 0 << endl;
  else if (n == 2)
    cout << m << endl;
  else
    cout << 2 * m << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int cases = 1;
  cin >> cases;
  while (cases--) Solve();
  return 0;
}
