#include <bits/stdc++.h>
using namespace std;
bool parz[100000];
const unsigned long long oo = 2000000000000000000;
const long long int mod = 1LL * (int(pow(10, 9)) + 7);
const int N = 200000;
long long int fibonach[100005];
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int tvpr(long long int n, long long int k) {
  return ((2 + (n - 1) * k) / 2) * n;
}
long long int tvpr1(long long int n) { return (n * (n + 1)) / 2; }
bool* erat(int n) {
  int i, k;
  bool* parz;
  parz = new bool[n + 1];
  for (i = 0; i <= n; i++) {
    parz[i] = 1;
  }
  for (i = 2; i <= n; i++) {
    if (parz[i]) {
      for (k = 2 * i; k <= n; k += i) {
        parz[k] = 0;
      }
    }
  }
  return parz;
}
long long int sqr(long long int a, long long int b, long long int c) {
  return ((a - b) * (a - b) + (b - c) * (b - c) + (c - a) * (c - a));
}
bool parzcheck(int n) {
  int i;
  for (i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) return true;
  }
  return false;
}
char z;
pair<int, int> lr[100000];
vector<int> ids;
struct lri {
  int index;
  pair<int, int> lr;
};
long long int solve(vector<long long int> a, vector<long long int> b,
                    vector<long long int> c) {
  long long int x, y, z, mn = oo;
  int i;
  for (i = 0; i < int(a.size()); i++) {
    x = a[i];
    auto y = lower_bound(b.begin(), b.end(), x);
    auto z = upper_bound(c.begin(), c.end(), x);
    z--;
    if (y == b.end() || z == c.begin())
      if (sqr(x, *y, *z) < mn) {
        mn = sqr(x, *y, *z);
      }
  }
  return mn;
}
bool ifnum(char a) {
  if (a >= '0' && a <= '9') {
    return true;
  } else {
    return false;
  }
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
void fibon() {
  int i;
  fibonach[0] = 1;
  fibonach[1] = 1;
  fibonach[2] = 2;
  for (i = 3; i <= 100000; i++) {
    fibonach[i] = 1LL * (fibonach[i - 1] + fibonach[i - 2]);
    fibonach[i] %= mod;
  }
}
int t, n, m, k;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  for (k = 0; k < t; k++) {
    cin >> n >> m;
    if (n == 1) {
      cout << 0 << endl;
    } else if (n == 2) {
      cout << m << endl;
    } else {
      cout << 2 * m << endl;
    }
  }
  return 0;
}
