#include <stdlib.h>

#include <iostream>
#include <string>
#include <vector>

using namespace std;

/* Structure for storing the definition of a prefix or suffix */
typedef struct {
    int coeff;      /* Term coefficient */
    int exp;        /* Exponent; 0 implies a constant term */
} term;

/* Remove leading white space in string */
void ltrim(string &word)
{
    int pos = word.find_first_not_of(" \n\r\t\v\f");
    word.erase(0, pos);
}

/*
 * Split a string along any of the characters in the delim string. The
 * delimiters themselves are included at the beginning of the split substrings.
 * Leading white space in all the strings is removed.
 */
vector<string> split(string word, char *delim)
{
    int pos = 0;
    vector<string> list;

    /* Remove leading white space and locate first delimiter */
    ltrim(word);
    pos = word.find_first_of(delim);
    
    /* Keep splitting word until no more delimiters found */
    while(pos != string::npos) {
            
        /* Create new string from everything to the left of delimiter */
        list.push_back(word.substr(0, pos));
        
        /* Remove everything to the left of delimiter */
        word.erase(0, pos);
        
        /* Skip over the delimiter left at beginning of string */
        pos = word.find_first_of(delim, 1);
    }
    
    /* Append the remaining delimiter-less word to list */
    ltrim(word);
    list.push_back(word);
    
    return list;
}

/* Parse single polynomail term */
term parse_term(string &word)
{
    vector<string> list;
    term term;

    /* Split word along "x" variable */
    list = split(word, "x");
    
    /* First part of word is the coefficient */
    string coeff = list[0];
    if(coeff == "-")
        term.coeff = -1;
    else if(coeff == "+")
        term.coeff = 1;
    else if(coeff == "")
        term.coeff = 1;
    else
        term.coeff = atoi(coeff.c_str());
        
    /* If second part of word is present, it is the exponent */
    if(list.size() > 1) {
        if(list[1] == "x")
            term.exp = 1;
        else
            term.exp = atoi(list[1].substr(2).c_str()); /* Skip over x^ */                
    } else {
        term.exp = 0;
    }
    
    return term;
}

/* Parse polynomail by splitting it into individual terms */
vector<term> parse_poly(void)
{
    vector<term> poly;
    string line;
    int i;
    
    /* Read in the rest of the line */
    getline(cin, line);
    
    /* Split line along polynomial terms */
    vector<string> list = split(line, "+-");

    /* Parse each polynomial term */
    for(i = 0; i < list.size(); i++) {        
        string word = list[i];
        term term;
    
        /* Only parse non-empty string terms */
        if(word.size()) {
            poly.push_back(parse_term(word));
        }
    }
        
    return poly;
}

/* Print out coefficient value */
void print_coeff(bool first, int coeff, bool always)
{
    /* Print out sign for first coefficient */
    if(first) {
        if(coeff < 0)
            cout << "-";
    }

    /* Print out sign for subsequent coefficients */
    else {
        if(coeff < 0)
            cout << "-";
        else
            cout << "+";
    }

    /* Print out coefficient absolute value */
    if(abs(coeff) != 1 || always)
        cout << abs(coeff);        
}

/* Print out polynomial with or without value substitiued for x */
void print_poly(vector<term> const &poly, bool eval, int x)
{
    int i;
    
    /* Iterate through each polynomial term */
    for(i = 0; i < poly.size(); i++) {
        term term = poly[i];
        
        /* Print out coefficient */
        print_coeff(i == 0, term.coeff, term.exp == 0);
        
        /* Print out "x" or x value if term is not a constant */
        if(term.exp > 0) {
            if(eval)
                cout << "(" << x << ")";
            else
                cout << "x";
        }

        /* Print out exponent */
        if(term.exp > 1)
            cout << "^" << term.exp;
    }
    
    cout << endl;
}

/* Compute derivative */
vector<term> compute_deriv(vector<term> poly)
{
    vector<term> deriv;
    int i;
    
    /* Process each term */
    for(i = 0; i < poly.size(); i++) {
        term term = poly[i];
        
        /* Constant terms are removed from derivatives */
        if(term.exp == 0)
            continue;
            
        /* Compute derivative */
        term.coeff *= term.exp;
        term.exp--;
        
        /* Add to derivative polynomial */
        deriv.push_back(term);
    }
    
    /* If derivative contains no terms, add single 0 constant term */
    if(deriv.size() == 0) {
        term term = { 0, 0 };
        deriv.push_back(term);
    }
    
    return deriv;
}

/* Compute x^p */
int power(int x, int p)
{
    int value = 1;
    
    for(; p; p--)
        value *= x;
    
    return value;
}

/* Compute polynomial value and printout each term value while at it */
int compute_value(vector<term> poly, int x)
{
    int total = 0, i;
    
    /* Process each term */
    for(i = 0; i < poly.size(); i++) {
        term term = poly[i];
        int value = term.coeff * power(x, term.exp);

        /* Print out computed term value and add to total */
        print_coeff(i == 0, value, true);
        total += value;
    }    
    
    cout << endl;
    return total;
}

/* Main body of program */
void process(void)
{
    int count, i;
    
    /* Throw exceptions on unexpected EOF */
    cin.exceptions(ios::eofbit);
       
    /* Read how many words are to be processed */
    cin >> count;

    /* Process each polynomial */
    for(i = 0; i < count; i++) {
        vector<term> poly, deriv;
        int x, value;
        
        /* Print out label */
        cout << "POLYNOMIAL " << i + 1 << endl;
        
        /* Read the x value and polynomial equation from stdin */
        cin >> x;
        poly = parse_poly();
        
        /* Printout polynomial as is */
        print_poly(poly, false, 0);

        /* Compute and printout derivative */
        deriv = compute_deriv(poly);
        print_poly(deriv, false, 0);
        
        /* Print derivative with value substituted */
        print_poly(deriv, true, x);
        
        /* Evaluate derivative, printing out each evaluated term */
        value = compute_value(deriv, x);
                
        /* Finally, print out the final value */
        cout << value << endl;
    }
}


/* Run program and print out any exceptions that occur */
int main(void)
{
    /* Run main body of code */
    try {
        process();
    }
    
    /* Catch any internally generated exceptions */
    catch(char const *e) {
        cerr << "Exception: " << e << endl;
    }
    
    /* Catch unexpected EOF on input */
    catch(ios::failure const &ee) {
        cerr << "Exception: Unexpected EOF on input" << endl;
    }
    
    return 0;
}