#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<climits>
#include<string>
using namespace std;
class solution
{
public:
void solve()
{
int x, cnt , n, sum, total;
cin >> x >> n;
cnt = x / 3;
sum = x * n;
total = sum - (cnt * n);
cout << total << "
";
}
};
int main()
{
solution ss;
int t;
cin >> t;
while (t--)
{
ss.solve();
}
return 0;
}