#include <stdlib.h>
#include <stdio.h>

typedef struct {
   int order;
   int coefficient;
} term_t;

typedef struct {
   int num_terms;
   term_t term[10];
   void (*init)(char *text_form);
   void (*display)(char *xval);
   void (*derivative)(void);
   void (*evaluate)(int xval);
   void (*sum)(void);
} poly_t;

poly_t poly;

void init (char *text_form)
{
   int sign;
   unsigned term;
   char *p = text_form;
   poly.num_terms = 0;

   for (p = text_form, term = 0; p[0] != '\0'; term++)
   {
      /* sign */
      sign = 1;
      if      (p[0] == '+') {            p++; }
      else if (p[0] == '-') { sign = -1; p++; }

      /* coefficient */
      if (p[0] >= '0' && p[0] <= '9')
      { poly.term[term].coefficient = p[0] - '0'; p++; }
      else
      { poly.term[term].coefficient = 1; }

      /* apply the sign */
      poly.term[term].coefficient *= sign;

      /* power */
      if (p[0] == 'x')
      {
         p++;
         if (p[0] == '^')
         { p++; poly.term[term].order = p[0] - '0'; p++; }
         else
         { poly.term[term].order = 1; }
      }
      else
      { 
         poly.term[term].order = 0;
      }
   }
   poly.num_terms = term;
}

void display (char *xval)
{
   int term;

   if (poly.num_terms == 0)
   {
      printf("0\n");
      fflush(stdout);
      return;
   }

   for (term = 0; term < poly.num_terms; term++)
   {
      if (term > 0 && poly.term[term].coefficient >= 0)
      { printf("+"); }

      if (   (poly.term[term].order == 0)
          || (   poly.term[term].coefficient != 1
              && poly.term[term].coefficient != -1))
      {
         printf("%d", poly.term[term].coefficient);
      }
      else if ( poly.term[term].coefficient == -1)
      {
         printf("-");
      }

      if (poly.term[term].order > 1)
      {
         printf("%s^%d",xval,poly.term[term].order);
      }
      else if (poly.term[term].order == 1)
      {
         printf("%s",xval);
      }
   }
   printf("\n");
   fflush(stdout);
}

void derivative (void)
{
   int term;

   for (term = 0; term < poly.num_terms; term++)
   {
      if (poly.term[term].order == 0)
      {
         poly.term[term].coefficient = 0;
         if (poly.num_terms > 1) { poly.num_terms--; }
         break;
      }
      poly.term[term].coefficient *= poly.term[term].order;
      poly.term[term].order--;
   } 
}

void evaluate (int xval)
{
   int term;

   for (term = 0; term < poly.num_terms; term++)
   {
      poly.term[term].coefficient =
         poly.term[term].coefficient *
         pow(xval, poly.term[term].order);
      poly.term[term].order = 0;
   } 
}

void sum (void)
{
   int term;
   int sum = 0;   

   for (term = 0; term < poly.num_terms; term++)
   {
      sum += poly.term[term].coefficient;
   }

   poly.term[0].coefficient = sum;
   poly.num_terms = 1;
}

int main (void)
{
   int datasets, i;

   scanf(" %d ", &datasets);

   for (i = 0; i < datasets; i++)
   {
      int  x;
      char poly_string[100];
      char xstring[10];

      /* read in 'x' and the polynomial */
      scanf(" %d %s ", &x, poly_string);

      /* print the heading */
      printf("POLYNOMIAL %d\n",i+1);

      /* Setup our new polynomial 'object' */
      poly.init = init;
      poly.display = display;
      poly.derivative = derivative;
      poly.evaluate = evaluate;
      poly.sum = sum;

      /* convert the polynomial into internal format */
      poly.init(poly_string);

      /* display it */
      poly.display("x");

      /* display the derivative */
      poly.derivative();
      poly.display("x");

      /* put in the x value */
      sprintf(xstring,"(%d)",x);
      poly.display(xstring); 

      /* evaluate x */
      poly.evaluate(x); 
      poly.display("x");

      /* sum it up */
      poly.sum(); 
      poly.display("x");
   }

   return 0;
}