#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int ar[10000001];
long long int gcd(long long int a, long long int b) {
  if (b == 0) {
    return a;
  }
  a %= b;
  return gcd(b, a);
}
long long int fexpo(long long int a, long long int b) {
  if (b == 0) return 1LL;
  if (b == 1) return a % mod;
  if (b == 2) return (a % mod) * (a % mod) % mod;
  if (b % 2 == 0)
    return fexpo(fexpo(a, b / 2), 2);
  else
    return a % mod * (fexpo(fexpo(a, (b - 1) / 2), 2) % mod);
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    if (n == 1) {
      cout << 0 << endl;
    } else if (n == 2) {
      cout << m << endl;
    } else
      cout << 2 + (2 * (m - 1)) << endl;
  }
}
