/*
        Problem:        Chemical reactions
        Description:    parse chemical equation and compare numbers of elements
        Class:          compilers
        Subclass:       expression
        Algorithm:      standard parsing O(n)
        Author:         Zdenek Dvorak
        Date:           Mar 11, 2002
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int ptable['Z'-'A'+1]['z'-'a'+2];

char ifle[20000], afle[20000];

ptable ifcnt, afcnt;

enum toks
 {
 TOK_END,
 TOK_ELEM,
 TOK_NUM,
 TOK_PLUS,
 TOK_OBR,
 TOK_CL
 };

typedef struct
 {
  enum toks tok;
  int n;
  int elem[2];
 } token;

typedef struct NODE
 {
  token val;
  struct NODE *lson, *rson;
 } node;

node nodes[40000];
int nnodes;

void get_token(char **in,token *tok)
 {
 switch (**in)
  {
   case 0: tok->tok=TOK_END; (*in)++; return;
   case '(': tok->tok=TOK_OBR; (*in)++; return;
   case ')': tok->tok=TOK_CL; (*in)++; return;
   case '+': tok->tok=TOK_PLUS; (*in)++; return;
   case 'A' ... 'Z':
    tok->tok=TOK_ELEM;
    tok->elem[0]=**in-'A';
    (*in)++;
    if ('a'<=**in && **in<='z')
     {
     tok->elem[1]=**in-'a'+1;
     (*in)++;
     }
    else
     tok->elem[1]=0;
    return;
   case '0' ... '9':
    tok->tok=TOK_NUM;
    tok->n=0;
    while ('0'<=**in&&**in<='9')
     {
      tok->n=10*tok->n+**in-'0';
      (*in)++;
     }
    return;
   default:
    fprintf(stderr,"unexpected char\n");
    exit(1);
  }
 }

void look_token(char **what,token *tok)
 {
 char *wh=*what;
 get_token(&wh,tok);
 }

node *parse_sequence(char **what)
 {
 token akt;
 node *sn,*myn,*amyn,*pmyn;
 myn = NULL;
 while (1)
  {
  look_token(what,&akt);
  switch (akt.tok)
   {
   case TOK_CL:
   case TOK_PLUS:
   case TOK_END:
    return myn;
   case TOK_OBR:
    get_token(what,&akt);
    sn=parse_sequence(what);
    get_token(what,&akt);
    if (akt.tok!=TOK_CL)
     {
     fprintf(stderr,"unexpected token\n");
     exit(1);
     }
    break;
   case TOK_ELEM:
    get_token(what,&akt);
    sn=nodes+nnodes++;
    sn->val=akt;
    break;
   default:
    fprintf(stderr,"unexpected token\n");
    exit(1);
   }
  look_token(what,&akt);
  if (akt.tok==TOK_NUM)
   {
    get_token(what,&akt);
    amyn=nodes+nnodes++;
    amyn->val=akt;
    amyn->lson=sn;
   }
  else
   amyn = sn;
  pmyn=nodes+nnodes++;
  pmyn->val.tok=TOK_PLUS;
  pmyn->lson=myn;
  pmyn->rson=amyn;
  myn=pmyn;
  }
 }

void count_node(node *node,ptable to,int mult)
 {
  if (!node)
   return;
  switch (node->val.tok)
   {
   case TOK_PLUS:
    count_node(node->lson,to,mult);
    count_node(node->rson,to,mult);
    break;
   case TOK_NUM:
    count_node(node->lson,to,mult*node->val.n);
    break;
   case TOK_ELEM:
    to[node->val.elem[0]][node->val.elem[1]]+=mult;
    break;
   default:
    fprintf(stderr,"unexpected node\n");
    exit(1);
   }
 }

void parse_block(char **what,ptable to)
 {
 token akt;
 int mult = 1;
 node *nd;
 look_token(what,&akt);
 switch (akt.tok)
  {
  case TOK_NUM:
   mult = akt.n;
   get_token(what,&akt);
   break;
  case TOK_ELEM:
  case TOK_OBR:
   break;
  default:
   fprintf(stderr,"unexpected token\n");
   exit(1);
  }
 nnodes=0;
 nd=parse_sequence(what);
 count_node(nd,to,mult);
 }

void parse(char *what,ptable to)
 {
 token akt;
 while (1)
  {
  parse_block (&what,to);
  get_token(&what,&akt);
  if (akt.tok==TOK_END)
   return;
  if (akt.tok!=TOK_PLUS)
   {
   fprintf(stderr,"+ expected\n");
   exit(1);
   }
  }
 }

int main(void)
 {
 int i, tc, n, j;
 //scanf("%d",&tc);
 //for (i=1;i<=tc;i++)
  {
  //printf("Testcase %d\n",i);
  scanf("%s",ifle);
  memset(ifcnt,0,sizeof(ptable));
  parse(ifle,ifcnt);
  scanf("%d",&n);
  for (j=0;j<n;j++)
   {
   scanf("%s",afle);
   memset(afcnt,0,sizeof(ptable));
   parse(afle,afcnt);
   printf("%s",ifle);
   printf(memcmp(ifcnt,afcnt,sizeof(ptable))?"!=":"==");
   printf("%s\n",afle);
   }
//  printf("\n");
  }
 return 0;
 }
