// Arup Guha
// 12/24/2015
// Solution to UCF 1987 HS Contest Problem: Polynomials

import java.util.*;

public class poly {

	// For our object.
	public int[] f;
	public int n;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		while (stdin.hasNext()) {

			// Read in all coefficients.
			int n = stdin.nextInt();
			int[] f = new int[n+1];
			for (int i=n; i>=0; i--)
				f[i] = stdin.nextInt();
			n = stdin.nextInt();
			int[] g = new int[n+1];
			for (int i=n; i>=0; i--)
				g[i] = stdin.nextInt();

			// Form polynomials.
			poly p1 = new poly(f);
			poly p2 = new poly(g);

			// Add them.
			poly res = p1.add(p2);

			// Output in their format.
			System.out.println("P(X) = "+p1);
			System.out.println("Q(X) = "+p2);
			System.out.println("P(X) + Q(X) = "+res);
			System.out.println();
		}
	}


	// Constructor.
	public poly(int[] coeff) {
		f = coeff;
		n = coeff.length-1;
	}

	// Returns this plus other.
	public poly add(poly other) {

		// Store result here.
		int[] res = new int[Math.max(n+1, other.n+1)];

		// Add in all valid coefficients.
		for (int i=0; i<res.length; i++) {
			if (i<=n) res[i] += f[i];
			if (i<=other.n) res[i] += other.f[i];
		}

		// Here is our result.
		return new poly(res);
	}

	// Returns a string representation of this polynomial.
	public String toString() {

		String res = "";

		// Find leading term.
		int i = n;
		while (i>=0 && f[i]==0) i--;
		if (i < 0) return res;
		res = res + term(f[i], i);
		i--;

		// Now, peel off the rest of the terms.
		while (i >= 0) {
			while (i>=0 && f[i]==0) i--;
			if (i < 0) break;
			res = res + " + " +term(f[i], i);
			i--;
		}

		// Here is our result.
		return res;
	}

	// Returns the string form of coeff*X^power.
	public static String term(int coeff, int power) {

		// Has printed exponent.
		if (power > 1) {
			if (coeff != 1) return coeff + " X ^ " + power;
			return "X ^ " + power;
		}

		// X term.
		if (power == 1) {
			if (coeff != 1) return coeff + " X";
			return "X";
		}

		// Constant term.
		return ""+coeff;
	}
}