UVA1586




Sample Input 

4 
C 
C6H5OH 
NH2CH2COOH 
C12H22O11

Sample Output 

12.010 
94.108 
75.070 
342.296



#include<stdio.h>
#include<math.h>
#include<string.h>
#include<time.h>
#define max 85
char atom[] = { 'C', 'H', 'O', 'N' };
double weight[] = { 12.01, 1.008, 16.00, 14.01 };

int main() {

 int T = 0;

 scanf("%d", &T);

 while (T--)
 {
  char s[max]; double total = 0.0;

  scanf("%s", s);

  for (int i = strlen(s)-1; i >=0; i--)
  {
   int x = 1, num = 0, t = 0;

   while('1' <= s[i] && s[i] <='9')
   {
    num += (s[i]-'0') * x;
    x *= 10;
    i--;
   }

   for (; t < 4 && s[i] != atom[t]; t++);

   num == 0 ? total += weight[t] : total += num*weight[t];
  }

  printf("%.3f\n", total);
 }
 return 0;
}