#include <bits/stdc++.h>
using namespace std;
bool prime[1000000 + 1];
bool seive() {
  long long int N = 1e6 + 1;
  memset(prime, true, sizeof(prime));
  prime[0] = false;
  prime[1] = false;
  for (long long int i = 2; i * i <= N; i++) {
    if (prime[i]) {
      for (long long int j = i * i; j <= N; j += i) {
        prime[j] = false;
      }
    }
  }
}
bool isPowerOfTwo(int x) { return (x && !(x & (x - 1))); }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    long long int k = ((2 > n - 1) ? n - 1 : 2);
    cout << k * m << endl;
  }
  return 0;
}
