#############################################
#
# This will save you the chore of typing in
# the correspondence between letters of the
# alphabet and elements of F27*.
#
# Get the nonzero elements and sort them
# lexicographically.
# First define the comparison function
# It represents polynomials as lists of
# coefficients and sorts.
def cmp(u, v):
"""Compare polynomials lexicographically."""
ul, vl = u.list(), v.list()
i, cp = 2, False
while i >= 0 and not cp:
if ul[i] < vl[i]:
r, cp = -1, True
elif ul[i] > vl[i]:
r, cp = 1, True
else:
i -= 1
if not cp:
r = 0
return int(r)