#include <bits/stdc++.h>
using namespace std;
int dw(string x, string y)
{
if(x == "km" && y == "m") return 1000;
else if(x == "km" && y == "mm") return 1000000;
else if(x == "m" && y == "mm") return 1000;
if(x == "kg" && y == "g") return 1000;
else if(x == "kg" && y == "mg") return 1000000;
else if(x == "g" && y == "mg") return 1000;
}
int num[1010];
string s1[1010], s2[1010];
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
char q;
cin >> num[i] >> s1[i] >> q >> s2[i];
}
for(int i = 1; i <= n; i++)
{
cout << num[i] * dw(s1[i], s2[i]) << endl;
}
return 0;
}