#include <bits/stdc++.h>
long long int mx = 2e+18;
const double pi = acos(-1.0);
using namespace std;
map<long long int, long long int> mp;
vector<pair<long long int, long long int> > vp;
set<long long int> st;
multiset<long long int> mst[1000][1000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int a, b, c, d, i, j, k, l, n, p, r, t, x, y, z;
  long long int f = 0, s = 0, s1 = 0, s2 = 0, sum = 0, m = 0, mn = 0, mn1 = 0,
                mn2 = 0;
  long long int arr[11], arr1[11];
  string ss, ss1, ss2, str;
  set<long long int>::iterator sit;
  map<long long int, long long int>::iterator mit;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> a >> b;
    if (a == 1) {
      cout << 0 << "\n";
    } else if (a == 2) {
      cout << b << "\n";
    } else {
      cout << b * 2 << "\n";
    }
  }
  return 0;
}
