#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
const long long int INF = 1e9;
const int MAX_N = 1e5 + 1;
void solve() {
  long long int n, m;
  cin >> n >> m;
  if (n == 1)
    cout << 0 << '\n';
  else if (n > 2 && m == 1)
    cout << 2 << '\n';
  else if (n < 2 && m == 1)
    cout << 1 << '\n';
  else if (n == 2)
    cout << m << '\n';
  else
    cout << 2 * m << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int tc;
  cin >> tc;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
  return 0;
}
