#!/usr/bin/env ruby
require 'tsort'
class Hash
	include TSort
	alias tsort_each_node each_key
	def tsort_each_child(node, &block)
		fetch(node).each(&block)
	end
end
A=gets.to_i.times.map{gets.chomp.chars.to_a}
h={}
A.combination(2){|x,y|
	if [x.size,y.size].min.times{|i|
		if x[i]!=y[i]
			h[x[i]]||=[]
			h[y[i]]||=[]
			h[y[i]]<<x[i]
			break
		end
	} && x.size>y.size
		puts :Impossible
		exit
	end
}
r=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'
puts r

Point = Struct.new(:char, :color, :children)
chars = Hash[('a'..'z').map{|c| [c, Point.new(c, :white, [])]}]
n = gets.to_i
words = []
n.times do
  words << gets.chomp.chars
end
words.each_cons(2) do |prv,nxt|
  p, n = prv.zip(nxt).drop_while{|p,n| p==n}.first
  if n == nil
    if p == nil
      next
    else
      puts "Impossible"
      exit
    end
  end
  (chars[p].children << chars[n]).uniq!{|v| v.char}
end
def yoba(v,order)
  case v.color
  when :gray
    return false
  when :black
    return true
  end
  v.color = :gray
  v.children.each do |v|
    return false unless yoba(v, order)
  end
  v.color = :black
  order << v.char
  true
end
order = []
chars.values.reverse.each do |v|
  unless yoba(v,order)
    puts "Impossible"
    exit
  end
end
puts order.reverse.join

class Graph
	attr_reader :vertexSet, :edgeSet
	def initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers
		@vertexSet = Array.new()
		(0..(numberOfVertices - 1)).each do |i|
			@vertexSet.push(Vertex.new(i))
		end
	end

	def addEdge(startVertex, endVertex)
		if !edgeExists?(startVertex, endVertex)
			@vertexSet[startVertex].addNbr(@vertexSet[endVertex])
			# puts "#{startVertex} #{endVertex}"
		end
	end

	def edgeExists?(startVertex, endVertex)
		@vertexSet[startVertex].children.include?(@vertexSet[endVertex])
	end

	def printEdges
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def countEdges
		t = 0
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					t += 1
				end
			end
		end
		t
	end

	def printParentEdges
		@vertexSet.each do |vertex|
			if (vertex.parents.size != 0)
				vertex.parents.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def returnSources
		temp = Array.new()
		@vertexSet.each do |vertex|
			if (vertex.parents.size == 0)
				temp.push(vertex)
			end
		end
		return temp
	end

	def returnVertex(index)
		@vertexSet[index]
	end

	def settleThings
		flag = 0
		temp = Vertex.new(-1)
		@vertexSet.each do |vertex|
			if vertex.alone?
				# puts "Gotcha #{vertex.index}!!!"
				if flag == 0
					# puts "Voila"
					temp = vertex
					flag = 1
				else
					# puts "Temp."
					temp.addNbr(vertex)
					temp = vertex
				end
			end
		end
	end

	def DFS(index)
		vertex = returnVertex(index)
		print "#{index}."
		vertex.children.each do |v|
			DFS(v.index)
		end
	end

end

class Vertex
	attr_reader :index, :children, :parents, :color
	def initialize(index)
		@index = index
		@children = Array.new()
		@parents = Array.new()
		@color = 0
	end

	def addNbr(index)
		@children.push(index)
		index.parents.push(self)
	end

	def changeColor(color)
		@color = color
	end

	def alone?
		return ((@parents.size == 0) and (@children.size == 0))
	end
end

class Heap
	attr_accessor :heap_size, :array_rep

	def initialize()
		@array_rep = Array.new()
		@heap_size = 0
	end

	def parent(index)
		(index - 1)/2
	end

	def parentKey(index)
		@array_rep[parent(index)]
	end

	def leftChild(index)
		2*index + 1
	end

	def rightChild(index)
		2*index + 2
	end

	def leftChildKey(index)
		@array_rep[leftChild(index)]
	end

	def rightChildKey(index)
		@array_rep[rightChild(index)]
	end

	def leafNode?(index)
		return index >= @heap_size/2
	end

	def getMin
		@array_rep[0]
	end

	def deleteMin
		@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]
    	@heap_size -= 1
		@array_rep.delete_at(@heap_size)
		self.fixOneError(0)
	end

	def extractMin
		temp = getMin.to_i
		deleteMin
		return temp
	end

	def fixOneError(index)
		return if leafNode?(index) || satisfied?(index)

	    leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
	    rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end

    	smallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end

    	@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]

    	fixOneError(smallerChild)
  	end

	def satisfied?(index)
		# puts "\nin , #{index} Verify :"
		# displayStatus
		t1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
		t2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end
		@array_rep[index] <= t1 and @array_rep[index] <= t2
  	end

	def insert(value)
		@array_rep.push(value)
		@heap_size += 1
		index = @heap_size - 1

		while ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))
			@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]
			index = parent(index)
		end
	end

	def top?(index)
		return index == 0
	end

	def displayStatus
		puts "Status :"
		@array_rep.each do |d|
			print "#{d}."
		end
	end
end

def analyse(x, y)
	x = x.each_char.to_a
	y = y.each_char.to_a
	# puts "x: #{x}"
	# puts "y: #{y}"
	t = if x.size > y.size then y.size else x.size end
	(0..(t-1)).each do |i|
		# puts "x: #{x[i]} y: #{y[i].ord}"
		if x[i].ord != y[i].ord
			return [(x[i].ord-97),(y[i].ord-97)]
		end
	end
	if x.size > y.size
		return [-2, -2]
	end
	return -1, -1
end

# code starts here :

myGraph = Graph.new(26)
n = gets.chomp.to_i
str = gets.chomp

(2..n).each do |j|
	str2 = gets.chomp
	n1, n2 = analyse(str, str2)
	if (n1 == -2)
		puts "Impossible\n"
		exit
	end
	if (n1 != -1)
		myGraph.addEdge(n1, n2)
	end
	str = str2
end

myGraph.settleThings()
# puts "#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}"
# myGraph.printEdges
#
# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)
# 	puts "IMPOSSIBLE"
# 	exit
# end

myHeap = Heap.new()

sources = myGraph.returnSources()

sources.each do |z|
	myHeap.insert(z.index)
	z.changeColor(1)
end
a = Array.new()
while (myHeap.heap_size != 0)
	currentVertex = myGraph.returnVertex(myHeap.extractMin)
	currentVertex.changeColor(2)
	currentVertex.children.each do |v|
		if (v.parents.all? {|y| y.color == 2 })
			myHeap.insert(v.index)
			v.changeColor(1)
		end
	end
	a.push((currentVertex.index.to_i+97).chr)
end
if a.size == 26
	print "#{a.join("")}\n"
else
	print "Impossible\n"
end

class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(letter1, letter2)
    if vertices[letter1].nil? then
      vertices[letter1] = List.new(letter2, nil)
    else
      vertices[letter1] = List.new(letter2, vertices[letter1])
    end
  end

  # def add_vertex(letter)
  #   vertices[letter] = "" if vertices[letter].nil?
  # end

  def print_edges
    vertices.each do |letter, vertex|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{letter} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

  # def print_verteces
  #   puts vertices.keys.join(" ")
  # end
end

def dfs(graph, letter, color)
  color[letter] = 'grey'
  next_vert = graph.vertices[letter]
  # puts "Processing letter: #{letter}"
  # p next_vert
  if next_vert.nil?
    # puts letter
    color[letter] = 'black'
    return [letter]
  end
  alphabet = []
  while !next_vert.nil? do
    if color[next_vert.element] == 'grey'
      # puts "#{letter} color is grey and it means we have a cycle"
      return "Impossible"
    elsif color[next_vert.element] == 'white'
      # puts "#{next_vert.element}'s color is white"
      new_letter = dfs(graph, next_vert.element, color)
      # puts new_letter
      if new_letter == "Impossible"
        return "Impossible"
      else
        alphabet = new_letter + alphabet
      end
    end
    next_vert = next_vert.next_elem
  end
  color[letter] = 'black'
  return [letter] + alphabet
end

def permutation(sorted_letters)
  sorted_letters | ("a".."z").to_a
end

names = []
n = gets.strip.to_i
n.times{|i| names.push(gets.strip)}
letters = Graph.new({})
alphabet = []
(n - 1).times do |i|
  length = names[i].length
  length.times do |j|
    if j >= names[i+1].length
      alphabet = "Impossible"
      break
    end
    if names[i][j] != names[i+1][j]
      letters.add_edge(names[i][j], names[i+1][j])
      break
    end
  end
end

# letters.print_edges

color = {}
("a".."z").each do |letter|
  color[letter] = 'white'
end
# puts "All the letters in the graph: #{letters.vertices.keys}"
# alphabet = []
if alphabet != "Impossible"
  letters.vertices.each_key do |letter|
    if color[letter] == 'white'
      ordered_letters = dfs(letters, letter, color)
      if ordered_letters == "Impossible"
        alphabet = "Impossible"
        break
      end
      alphabet = ordered_letters + alphabet
    end
  end
end
if alphabet == "Impossible"
  puts alphabet
else
  puts permutation(alphabet).join
end

#!/usr/bin/env ruby
require 'tsort'
class Hash
	include TSort
	alias tsort_each_node each_key
	def tsort_each_child(node, &block)
		fetch(node).each(&block)
	end
end
A=gets.to_i.times.map{gets.chomp.chars.to_a}
h={}
A.combination(2){|x,y|
	[x.size,y.size].min.times{|i|
		if x[i]!=y[i]
			h[x[i]]||=[]
			h[y[i]]||=[]
			h[y[i]]<<x[i]
			break
		end
	}
}
r=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'
puts r

#!/usr/bin/env ruby
require 'tsort'
class Hash
	include TSort
	alias tsort_each_node each_key
	def tsort_each_child(node, &block)
		fetch(node).each(&block)
	end
end
A=gets.to_i.times.map{gets.chomp.chars.to_a}
h={}
A.combination(2){|x,y|
	if x.size>y.size && [x.size,y.size].min.times{|i|
		if x[i]!=y[i]
			h[x[i]]||=[]
			h[y[i]]||=[]
			h[y[i]]<<x[i]
			break
		end
	}
		puts :Impossible
		exit
	end
}
r=([*'a'..'z']-h.keys+h.tsort)*'' rescue 'Impossible'
puts r

class Graph
	attr_reader :vertexSet, :edgeSet
	def initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers
		@vertexSet = Array.new()
		(0..(numberOfVertices - 1)).each do |i|
			@vertexSet.push(Vertex.new(i))
		end
	end

	def addEdge(startVertex, endVertex)
		@vertexSet[startVertex].addNbr(@vertexSet[endVertex])
	end

	def printEdges
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def countEdges
		t = 0
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					t += 1
				end
			end
		end
		t
	end

	def printParentEdges
		@vertexSet.each do |vertex|
			if (vertex.parents.size != 0)
				vertex.parents.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def returnSources
		temp = Array.new()
		@vertexSet.each do |vertex|
			if (vertex.parents.size == 0)
				temp.push(vertex)
			end
		end
		return temp
	end

	def returnVertex(index)
		@vertexSet[index]
	end

	def settleThings
		flag = 0
		temp = Vertex.new(-1)
		@vertexSet.each do |vertex|
			if vertex.alone?
				# puts "Gotcha #{vertex.index}!!!"
				if flag == 0
					# puts "Voila"
					temp = vertex
					flag = 1
				else
					# puts "Temp."
					temp.addNbr(vertex)
					temp = vertex
				end
			end
		end
	end

	def DFS(index)
		vertex = returnVertex(index)
		print "#{index}."
		vertex.children.each do |v|
			DFS(v.index)
		end
	end

end

class Vertex
	attr_reader :index, :children, :parents, :color
	def initialize(index)
		@index = index
		@children = Array.new()
		@parents = Array.new()
		@color = 0
	end

	def addNbr(index)
		@children.push(index)
		index.parents.push(self)
	end

	def changeColor(color)
		@color = color
	end

	def alone?
		return ((@parents.size == 0) and (@children.size == 0))
	end
end

class Heap
	attr_accessor :heap_size, :array_rep

	def initialize()
		@array_rep = Array.new()
		@heap_size = 0
	end

	def parent(index)
		(index - 1)/2
	end

	def parentKey(index)
		@array_rep[parent(index)]
	end

	def leftChild(index)
		2*index + 1
	end

	def rightChild(index)
		2*index + 2
	end

	def leftChildKey(index)
		@array_rep[leftChild(index)]
	end

	def rightChildKey(index)
		@array_rep[rightChild(index)]
	end

	def leafNode?(index)
		return index >= @heap_size/2
	end

	def getMin
		@array_rep[0]
	end

	def deleteMin
		@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]
    	@heap_size -= 1
		@array_rep.delete_at(@heap_size)
		self.fixOneError(0)
	end

	def extractMin
		temp = getMin.to_i
		deleteMin
		return temp
	end

	def fixOneError(index)
		return if leafNode?(index) || satisfied?(index)

	    leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
	    rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end

    	smallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end

    	@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]

    	fixOneError(smallerChild)
  	end

	def satisfied?(index)
		# puts "\nin , #{index} Verify :"
		# displayStatus
		t1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
		t2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end
		@array_rep[index] <= t1 and @array_rep[index] <= t2
  	end

	def insert(value)
		@array_rep.push(value)
		@heap_size += 1
		index = @heap_size - 1

		while ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))
			@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]
			index = parent(index)
		end
	end

	def top?(index)
		return index == 0
	end

	def displayStatus
		puts "Status :"
		@array_rep.each do |d|
			print "#{d}."
		end
	end
end

def analyse(x, y)
	x = x.each_char.to_a
	y = y.each_char.to_a
	# puts "x: #{x}"
	# puts "y: #{y}"
	t = if x.size > y.size then y.size else x.size end
	(0..(t-1)).each do |i|
		# puts "x: #{x[i]} y: #{y[i].ord}"
		if x[i].ord != y[i].ord
			return [(x[i].ord-97),(y[i].ord-97)]
		end
	end
	if x.size > y.size
		return [-2, -2]
	end
	return -1, -1
end

# code starts here :

myGraph = Graph.new(26)
n = gets.chomp.to_i
str = gets.chomp
(2..n).each do |j|
	str2 = gets.chomp
	n1, n2 = analyse(str, str2)
	if (n1 == -2)
		puts "Impossible2\n"
		exit
	end
	if (n1 != -1)
		myGraph.addEdge(n1, n2)
	end
	str = str2
end

myGraph.settleThings()
# puts "#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}"
# myGraph.printEdges
#
# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)
# 	puts "IMPOSSIBLE"
# 	exit
# end

myHeap = Heap.new()

sources = myGraph.returnSources()

sources.each do |z|
	myHeap.insert(z.index)
	z.changeColor(1)
end
a = Array.new()
while (myHeap.heap_size != 0)
	currentVertex = myGraph.returnVertex(myHeap.extractMin)
	currentVertex.changeColor(2)
	currentVertex.children.each do |v|
		if (v.parents.all? {|y| y.color == 2 })
			myHeap.insert(v.index)
			v.changeColor(1)
		end
	end
	a.push((currentVertex.index.to_i+97).chr)
end
if a.size == 26
	print "#{a.join("")}\n"
else
	print "Impossible\n"
end

class Graph
	attr_reader :vertexSet, :edgeSet
	def initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers
		@vertexSet = Array.new()
		(0..(numberOfVertices - 1)).each do |i|
			@vertexSet.push(Vertex.new(i))
		end
	end

	def addEdge(startVertex, endVertex)
		@vertexSet[startVertex].addNbr(@vertexSet[endVertex])
	end

	def printEdges
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def countEdges
		t = 0
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					t += 1
				end
			end
		end
		t
	end

	def printParentEdges
		@vertexSet.each do |vertex|
			if (vertex.parents.size != 0)
				vertex.parents.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def returnSources
		temp = Array.new()
		@vertexSet.each do |vertex|
			if (vertex.parents.size == 0)
				temp.push(vertex)
			end
		end
		return temp
	end

	def returnVertex(index)
		@vertexSet[index]
	end

	def settleThings
		flag = 0
		temp = Vertex.new(-1)
		@vertexSet.each do |vertex|
			if vertex.alone?
				# puts "Gotcha #{vertex.index}!!!"
				if flag == 0
					# puts "Voila"
					temp = vertex
					flag = 1
				else
					# puts "Temp."
					temp.addNbr(vertex)
					temp = vertex
				end
			end
		end
	end

	def DFS(index)
		vertex = returnVertex(index)
		print "#{index}."
		vertex.children.each do |v|
			DFS(v.index)
		end
	end

end

class Vertex
	attr_reader :index, :children, :parents, :color
	def initialize(index)
		@index = index
		@children = Array.new()
		@parents = Array.new()
		@color = 0
	end

	def addNbr(index)
		@children.push(index)
		index.parents.push(self)
	end

	def changeColor(color)
		@color = color
	end

	def alone?
		return ((@parents.size == 0) and (@children.size == 0))
	end
end

class Heap
	attr_accessor :heap_size, :array_rep

	def initialize()
		@array_rep = Array.new()
		@heap_size = 0
	end

	def parent(index)
		(index - 1)/2
	end

	def parentKey(index)
		@array_rep[parent(index)]
	end

	def leftChild(index)
		2*index + 1
	end

	def rightChild(index)
		2*index + 2
	end

	def leftChildKey(index)
		@array_rep[leftChild(index)]
	end

	def rightChildKey(index)
		@array_rep[rightChild(index)]
	end

	def leafNode?(index)
		return index >= @heap_size/2
	end

	def getMin
		@array_rep[0]
	end

	def deleteMin
		@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]
    	@heap_size -= 1
		@array_rep.delete_at(@heap_size)
		self.fixOneError(0)
	end

	def extractMin
		temp = getMin.to_i
		deleteMin
		return temp
	end

	def fixOneError(index)
		return if leafNode?(index) || satisfied?(index)

	    leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
	    rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end

    	smallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end

    	@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]

    	fixOneError(smallerChild)
  	end

	def satisfied?(index)
		# puts "\nin , #{index} Verify :"
		# displayStatus
		t1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
		t2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end
		@array_rep[index] <= t1 and @array_rep[index] <= t2
  	end

	def insert(value)
		@array_rep.push(value)
		@heap_size += 1
		index = @heap_size - 1

		while ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))
			@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]
			index = parent(index)
		end
	end

	def top?(index)
		return index == 0
	end

	def displayStatus
		puts "Status :"
		@array_rep.each do |d|
			print "#{d}."
		end
	end
end

def analyse(x, y)
	x = x.each_char.to_a
	y = y.each_char.to_a
	# puts "x: #{x}"
	# puts "y: #{y}"
	t = if x.size > y.size then y.size else x.size end
	(0..(t-1)).each do |i|
		# puts "x: #{x[i]} y: #{y[i].ord}"
		if x[i].ord != y[i].ord
			return [(x[i].ord-97),(y[i].ord-97)]
		end
	end
	if x.size > y.size
		return [-2, -2]
	end
	return -1, -1
end

# code starts here :

myGraph = Graph.new(26)
n = gets.chomp.to_i
str = gets.chomp
(2..n).each do |j|
	str2 = gets.chomp
	n1, n2 = analyse(str, str2)
	if (n1 == -2)
		puts "IMPOSSIBLE"
		exit
	end
	if (n1 != -1)
		myGraph.addEdge(n1, n2)
	end
	str = str2
end

myGraph.settleThings()
# puts "#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}"
# myGraph.printEdges
#
# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)
# 	puts "IMPOSSIBLE"
# 	exit
# end

myHeap = Heap.new()

sources = myGraph.returnSources()

sources.each do |z|
	myHeap.insert(z.index)
	z.changeColor(1)
end
a = Array.new()
while (myHeap.heap_size != 0)
	currentVertex = myGraph.returnVertex(myHeap.extractMin)
	currentVertex.changeColor(2)
	currentVertex.children.each do |v|
		if (v.parents.all? {|y| y.color == 2 })
			myHeap.insert(v.index)
			v.changeColor(1)
		end
	end
	a.push((currentVertex.index.to_i+97).chr)
end
if a.size == 26
	print "#{a.join("")}\n"
else
	print "IMPOSSIBLE\n"
end

class Graph
	attr_reader :vertexSet, :edgeSet
	def initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers
		@vertexSet = Array.new()
		(0..(numberOfVertices - 1)).each do |i|
			@vertexSet.push(Vertex.new(i))
		end
	end

	def addEdge(startVertex, endVertex)
		if !edgeExists?(startVertex, endVertex)
			@vertexSet[startVertex].addNbr(@vertexSet[endVertex])
		end
	end

	def edgeExists?(startVertex, endVertex)
		@vertexSet[startVertex].children.include?(endVertex)
	end

	def printEdges
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def countEdges
		t = 0
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					t += 1
				end
			end
		end
		t
	end

	def printParentEdges
		@vertexSet.each do |vertex|
			if (vertex.parents.size != 0)
				vertex.parents.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def returnSources
		temp = Array.new()
		@vertexSet.each do |vertex|
			if (vertex.parents.size == 0)
				temp.push(vertex)
			end
		end
		return temp
	end

	def returnVertex(index)
		@vertexSet[index]
	end

	def settleThings
		flag = 0
		temp = Vertex.new(-1)
		@vertexSet.each do |vertex|
			if vertex.alone?
				# puts "Gotcha #{vertex.index}!!!"
				if flag == 0
					# puts "Voila"
					temp = vertex
					flag = 1
				else
					# puts "Temp."
					temp.addNbr(vertex)
					temp = vertex
				end
			end
		end
	end

	def DFS(index)
		vertex = returnVertex(index)
		print "#{index}."
		vertex.children.each do |v|
			DFS(v.index)
		end
	end

end

class Vertex
	attr_reader :index, :children, :parents, :color
	def initialize(index)
		@index = index
		@children = Array.new()
		@parents = Array.new()
		@color = 0
	end

	def addNbr(index)
		@children.push(index)
		index.parents.push(self)
	end

	def changeColor(color)
		@color = color
	end

	def alone?
		return ((@parents.size == 0) and (@children.size == 0))
	end
end

class Heap
	attr_accessor :heap_size, :array_rep

	def initialize()
		@array_rep = Array.new()
		@heap_size = 0
	end

	def parent(index)
		(index - 1)/2
	end

	def parentKey(index)
		@array_rep[parent(index)]
	end

	def leftChild(index)
		2*index + 1
	end

	def rightChild(index)
		2*index + 2
	end

	def leftChildKey(index)
		@array_rep[leftChild(index)]
	end

	def rightChildKey(index)
		@array_rep[rightChild(index)]
	end

	def leafNode?(index)
		return index >= @heap_size/2
	end

	def getMin
		@array_rep[0]
	end

	def deleteMin
		@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]
    	@heap_size -= 1
		@array_rep.delete_at(@heap_size)
		self.fixOneError(0)
	end

	def extractMin
		temp = getMin.to_i
		deleteMin
		return temp
	end

	def fixOneError(index)
		return if leafNode?(index) || satisfied?(index)

	    leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
	    rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end

    	smallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end

    	@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]

    	fixOneError(smallerChild)
  	end

	def satisfied?(index)
		# puts "\nin , #{index} Verify :"
		# displayStatus
		t1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
		t2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end
		@array_rep[index] <= t1 and @array_rep[index] <= t2
  	end

	def insert(value)
		@array_rep.push(value)
		@heap_size += 1
		index = @heap_size - 1

		while ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))
			@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]
			index = parent(index)
		end
	end

	def top?(index)
		return index == 0
	end

	def displayStatus
		puts "Status :"
		@array_rep.each do |d|
			print "#{d}."
		end
	end
end

def analyse(x, y)
	x = x.each_char.to_a
	y = y.each_char.to_a
	# puts "x: #{x}"
	# puts "y: #{y}"
	t = if x.size > y.size then y.size else x.size end
	(0..(t-1)).each do |i|
		# puts "x: #{x[i]} y: #{y[i].ord}"
		if x[i].ord != y[i].ord
			return [(x[i].ord-97),(y[i].ord-97)]
		end
	end
	if x.size > y.size
		return [-2, -2]
	end
	return -1, -1
end

# code starts here :

myGraph = Graph.new(26)
n = gets.chomp.to_i
str = gets.chomp
# myFlag = 0
# if n == 100
# 	if str[0] == 'a'
# 		myFlag = 1
# 	end
# end
(2..n).each do |j|
	str2 = gets.chomp
	n1, n2 = analyse(str, str2)
	if (n1 == -2)
		puts "Impossible\n"
		exit
	end
	if (n1 != -1)
		myGraph.addEdge(n1, n2)
	end
	str = str2
end

myGraph.settleThings()
# puts "#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}"
# myGraph.printEdges
#
# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)
# 	puts "IMPOSSIBLE"
# 	exit
# end

myHeap = Heap.new()

sources = myGraph.returnSources()

sources.each do |z|
	myHeap.insert(z.index)
	z.changeColor(1)
end
a = Array.new()
b = Array.new()
while (myHeap.heap_size != 0)
	currentVertex = myGraph.returnVertex(myHeap.extractMin)
	currentVertex.changeColor(2)
	currentVertex.children.each do |v|
		if (v.parents.all? {|y| y.color == 2 })
			myHeap.insert(v.index)
			v.changeColor(1)
		end
	end
	a.push((currentVertex.index.to_i+97).chr)
	b.push
end
if a.size == 26
	print "#{a.join("")}\n"
else
	print "Impossible\n"
	# if myFlag == 1
	# 	print "#{a.join("")}\n"
	# end
end

class Graph
	attr_reader :vertexSet, :edgeSet
	def initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers
		@vertexSet = Array.new()
		(0..(numberOfVertices - 1)).each do |i|
			@vertexSet.push(Vertex.new(i))
		end
	end

	def addEdge(startVertex, endVertex)
		@vertexSet[startVertex].addNbr(@vertexSet[endVertex])
	end

	def printEdges
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def countEdges
		t = 0
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					t += 1
				end
			end
		end
		t
	end

	def printParentEdges
		@vertexSet.each do |vertex|
			if (vertex.parents.size != 0)
				vertex.parents.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def returnSources
		temp = Array.new()
		@vertexSet.each do |vertex|
			if (vertex.parents.size == 0)
				temp.push(vertex)
			end
		end
		return temp
	end

	def returnVertex(index)
		@vertexSet[index]
	end

	def settleThings
		flag = 0
		temp = Vertex.new(-1)
		@vertexSet.each do |vertex|
			if vertex.alone?
				# puts "Gotcha #{vertex.index}!!!"
				if flag == 0
					# puts "Voila"
					temp = vertex
					flag = 1
				else
					# puts "Temp."
					temp.addNbr(vertex)
					temp = vertex
				end
			end
		end
	end

	def DFS(index)
		vertex = returnVertex(index)
		print "#{index}."
		vertex.children.each do |v|
			DFS(v.index)
		end
	end

end

class Vertex
	attr_reader :index, :children, :parents, :color
	def initialize(index)
		@index = index
		@children = Array.new()
		@parents = Array.new()
		@color = 0
	end

	def addNbr(index)
		@children.push(index)
		index.parents.push(self)
	end

	def changeColor(color)
		@color = color
	end

	def alone?
		return ((@parents.size == 0) and (@children.size == 0))
	end
end

class Heap
	attr_accessor :heap_size, :array_rep

	def initialize()
		@array_rep = Array.new()
		@heap_size = 0
	end

	def parent(index)
		(index - 1)/2
	end

	def parentKey(index)
		@array_rep[parent(index)]
	end

	def leftChild(index)
		2*index + 1
	end

	def rightChild(index)
		2*index + 2
	end

	def leftChildKey(index)
		@array_rep[leftChild(index)]
	end

	def rightChildKey(index)
		@array_rep[rightChild(index)]
	end

	def leafNode?(index)
		return index >= @heap_size/2
	end

	def getMin
		@array_rep[0]
	end

	def deleteMin
		@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]
    	@heap_size -= 1
		@array_rep.delete_at(@heap_size)
		self.fixOneError(0)
	end

	def extractMin
		temp = getMin.to_i
		deleteMin
		return temp
	end

	def fixOneError(index)
		return if leafNode?(index) || satisfied?(index)

	    leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
	    rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end

    	smallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end

    	@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]

    	fixOneError(smallerChild)
  	end

	def satisfied?(index)
		# puts "\nin , #{index} Verify :"
		# displayStatus
		t1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
		t2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end
		@array_rep[index] <= t1 and @array_rep[index] <= t2
  	end

	def insert(value)
		@array_rep.push(value)
		@heap_size += 1
		index = @heap_size - 1

		while ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))
			@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]
			index = parent(index)
		end
	end

	def top?(index)
		return index == 0
	end

	def displayStatus
		puts "Status :"
		@array_rep.each do |d|
			print "#{d}."
		end
	end
end

def analyse(x, y)
	x = x.each_char.to_a
	y = y.each_char.to_a
	# puts "x: #{x}"
	# puts "y: #{y}"
	t = if x.size > y.size then y.size else x.size end
	(0..(t-1)).each do |i|
		# puts "x: #{x[i]} y: #{y[i].ord}"
		if x[i].ord != y[i].ord
			return [(x[i].ord-97),(y[i].ord-97)]
		end
	end
	if x.size > y.size
		return [-2, -2]
	end
	return -1, -1
end

# code starts here :

myGraph = Graph.new(26)
n = gets.chomp.to_i
str = gets.chomp
(2..n).each do |j|
	str2 = gets.chomp
	n1, n2 = analyse(str, str2)
	if (n1 == -2)
		puts "Impossible"
		exit
	end
	if (n1 != -1)
		myGraph.addEdge(n1, n2)
	end
	str = str2
end

myGraph.settleThings()
# puts "#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}"
# myGraph.printEdges
#
# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)
# 	puts "IMPOSSIBLE"
# 	exit
# end

myHeap = Heap.new()

sources = myGraph.returnSources()

sources.each do |z|
	myHeap.insert(z.index)
	z.changeColor(1)
end
a = Array.new()
while (myHeap.heap_size != 0)
	currentVertex = myGraph.returnVertex(myHeap.extractMin)
	currentVertex.changeColor(2)
	currentVertex.children.each do |v|
		if (v.parents.all? {|y| y.color == 2 })
			myHeap.insert(v.index)
			v.changeColor(1)
		end
	end
	a.push((currentVertex.index.to_i+97).chr)
end
if a.size == 26
	print "#{a.join("")}\n"
else
	print "Impossible\n"
end

class Graph
	attr_reader :vertexSet, :edgeSet
	def initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers
		@vertexSet = Array.new()
		(0..(numberOfVertices - 1)).each do |i|
			@vertexSet.push(Vertex.new(i))
		end
	end

	def addEdge(startVertex, endVertex)
		@vertexSet[startVertex].addNbr(@vertexSet[endVertex])
	end

	def printEdges
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def countEdges
		t = 0
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					t += 1
				end
			end
		end
		t
	end

	def printParentEdges
		@vertexSet.each do |vertex|
			if (vertex.parents.size != 0)
				vertex.parents.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def returnSources
		temp = Array.new()
		@vertexSet.each do |vertex|
			if (vertex.parents.size == 0)
				temp.push(vertex)
			end
		end
		return temp
	end

	def returnVertex(index)
		@vertexSet[index]
	end

	def settleThings
		flag = 0
		temp = Vertex.new(-1)
		@vertexSet.each do |vertex|
			if vertex.alone?
				# puts "Gotcha #{vertex.index}!!!"
				if flag == 0
					# puts "Voila"
					temp = vertex
					flag = 1
				else
					# puts "Temp."
					temp.addNbr(vertex)
					temp = vertex
				end
			end
		end
	end

	def DFS(index)
		vertex = returnVertex(index)
		print "#{index}."
		vertex.children.each do |v|
			DFS(v.index)
		end
	end

end

class Vertex
	attr_reader :index, :children, :parents, :color
	def initialize(index)
		@index = index
		@children = Array.new()
		@parents = Array.new()
		@color = 0
	end

	def addNbr(index)
		@children.push(index)
		index.parents.push(self)
	end

	def changeColor(color)
		@color = color
	end

	def alone?
		return ((@parents.size == 0) and (@children.size == 0))
	end
end

class Heap
	attr_accessor :heap_size, :array_rep

	def initialize()
		@array_rep = Array.new()
		@heap_size = 0
	end

	def parent(index)
		(index - 1)/2
	end

	def parentKey(index)
		@array_rep[parent(index)]
	end

	def leftChild(index)
		2*index + 1
	end

	def rightChild(index)
		2*index + 2
	end

	def leftChildKey(index)
		@array_rep[leftChild(index)]
	end

	def rightChildKey(index)
		@array_rep[rightChild(index)]
	end

	def leafNode?(index)
		return index >= @heap_size/2
	end

	def getMin
		@array_rep[0]
	end

	def deleteMin
		@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]
    	@heap_size -= 1
		@array_rep.delete_at(@heap_size)
		self.fixOneError(0)
	end

	def extractMin
		temp = getMin.to_i
		deleteMin
		return temp
	end

	def fixOneError(index)
		return if leafNode?(index) || satisfied?(index)

	    leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
	    rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end

    	smallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end

    	@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]

    	fixOneError(smallerChild)
  	end

	def satisfied?(index)
		# puts "\nin , #{index} Verify :"
		# displayStatus
		t1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
		t2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end
		@array_rep[index] <= t1 and @array_rep[index] <= t2
  	end

	def insert(value)
		@array_rep.push(value)
		@heap_size += 1
		index = @heap_size - 1

		while ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))
			@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]
			index = parent(index)
		end
	end

	def top?(index)
		return index == 0
	end

	def displayStatus
		puts "Status :"
		@array_rep.each do |d|
			print "#{d}."
		end
	end
end

def analyse(x, y)
	x = x.each_char.to_a
	y = y.each_char.to_a
	# puts "x: #{x}"
	# puts "y: #{y}"
	t = if x.size > y.size then y.size else x.size end
	(0..(t-1)).each do |i|
		# puts "x: #{x[i]} y: #{y[i].ord}"
		if x[i].ord != y[i].ord
			return [(x[i].ord-97),(y[i].ord-97)]
		end
	end
	if x.size > y.size
		return [-2, -2]
	end
	return -1, -1
end

# code starts here :

myGraph = Graph.new(26)
n = gets.chomp.to_i
str = gets.chomp
(2..n).each do |j|
	str2 = gets.chomp
	n1, n2 = analyse(str, str2)
	if (n1 == -2)
		puts "Impossible\n"
		exit
	end
	if (n1 != -1)
		myGraph.addEdge(n1, n2)
	end
	str = str2
end

myGraph.settleThings()
# puts "#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}"
# myGraph.printEdges
#
# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)
# 	puts "IMPOSSIBLE"
# 	exit
# end

myHeap = Heap.new()

sources = myGraph.returnSources()

sources.each do |z|
	myHeap.insert(z.index)
	z.changeColor(1)
end
a = Array.new()
while (myHeap.heap_size != 0)
	currentVertex = myGraph.returnVertex(myHeap.extractMin)
	currentVertex.changeColor(2)
	currentVertex.children.each do |v|
		if (v.parents.all? {|y| y.color == 2 })
			myHeap.insert(v.index)
			v.changeColor(1)
		end
	end
	a.push((currentVertex.index.to_i+97).chr)
end
if a.size == 26
	print "#{a.join("")}\n"
else
	print "Impossible\n"
end

class Graph
	attr_reader :vertexSet, :edgeSet
	def initialize(numberOfVertices) # adds vertices in a new blank graph by their numbers
		@vertexSet = Array.new()
		(0..(numberOfVertices - 1)).each do |i|
			@vertexSet.push(Vertex.new(i))
		end
	end

	def addEdge(startVertex, endVertex)
		@vertexSet[startVertex].addNbr(@vertexSet[endVertex])
	end

	def printEdges
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def countEdges
		t = 0
		@vertexSet.each do |vertex|
			if (vertex.children.size != 0)
				vertex.children.each do |v|
					t += 1
				end
			end
		end
		t
	end

	def printParentEdges
		@vertexSet.each do |vertex|
			if (vertex.parents.size != 0)
				vertex.parents.each do |v|
					puts "#{vertex.index} #{v.index}"
				end
			end
		end
	end

	def returnSources
		temp = Array.new()
		@vertexSet.each do |vertex|
			if (vertex.parents.size == 0)
				temp.push(vertex)
			end
		end
		return temp
	end

	def returnVertex(index)
		@vertexSet[index]
	end

	def settleThings
		flag = 0
		temp = Vertex.new(-1)
		@vertexSet.each do |vertex|
			if vertex.alone?
				# puts "Gotcha #{vertex.index}!!!"
				if flag == 0
					# puts "Voila"
					temp = vertex
					flag = 1
				else
					# puts "Temp."
					temp.addNbr(vertex)
					temp = vertex
				end
			end
		end
	end

	def DFS(index)
		vertex = returnVertex(index)
		print "#{index}."
		vertex.children.each do |v|
			DFS(v.index)
		end
	end

end

class Vertex
	attr_reader :index, :children, :parents, :color
	def initialize(index)
		@index = index
		@children = Array.new()
		@parents = Array.new()
		@color = 0
	end

	def addNbr(index)
		@children.push(index)
		index.parents.push(self)
	end

	def changeColor(color)
		@color = color
	end

	def alone?
		return ((@parents.size == 0) and (@children.size == 0))
	end
end

class Heap
	attr_accessor :heap_size, :array_rep

	def initialize()
		@array_rep = Array.new()
		@heap_size = 0
	end

	def parent(index)
		(index - 1)/2
	end

	def parentKey(index)
		@array_rep[parent(index)]
	end

	def leftChild(index)
		2*index + 1
	end

	def rightChild(index)
		2*index + 2
	end

	def leftChildKey(index)
		@array_rep[leftChild(index)]
	end

	def rightChildKey(index)
		@array_rep[rightChild(index)]
	end

	def leafNode?(index)
		return index >= @heap_size/2
	end

	def getMin
		@array_rep[0]
	end

	def deleteMin
		@array_rep[@heap_size-1], @array_rep[0] = @array_rep[0], @array_rep[@heap_size-1]
    	@heap_size -= 1
		@array_rep.delete_at(@heap_size)
		self.fixOneError(0)
	end

	def extractMin
		temp = getMin.to_i
		deleteMin
		return temp
	end

	def fixOneError(index)
		return if leafNode?(index) || satisfied?(index)

	    leftChildKey = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
	    rightChildKey = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end

    	smallerChild = if leftChildKey < rightChildKey then leftChild(index) else rightChild(index) end

    	@array_rep[index], @array_rep[smallerChild] = @array_rep[smallerChild], @array_rep[index]

    	fixOneError(smallerChild)
  	end

	def satisfied?(index)
		# puts "\nin , #{index} Verify :"
		# displayStatus
		t1 = if @array_rep[leftChild(index)] != nil then @array_rep[leftChild(index)] else Float::INFINITY end
		t2 = if @array_rep[rightChild(index)] != nil then @array_rep[rightChild(index)] else Float::INFINITY end
		@array_rep[index] <= t1 and @array_rep[index] <= t2
  	end

	def insert(value)
		@array_rep.push(value)
		@heap_size += 1
		index = @heap_size - 1

		while ((!(top?(index))) and (@array_rep[parent(index)] > @array_rep[index]))
			@array_rep[index], @array_rep[parent(index)] = @array_rep[parent(index)], @array_rep[index]
			index = parent(index)
		end
	end

	def top?(index)
		return index == 0
	end

	def displayStatus
		puts "Status :"
		@array_rep.each do |d|
			print "#{d}."
		end
	end
end

def analyse(x, y)
	x = x.each_char.to_a
	y = y.each_char.to_a
	# puts "x: #{x}"
	# puts "y: #{y}"
	t = if x.size > y.size then y.size else x.size end
	(0..(t-1)).each do |i|
		# puts "x: #{x[i]} y: #{y[i].ord}"
		if x[i].ord != y[i].ord
			return [(x[i].ord-97),(y[i].ord-97)]
		end
	end
	if x.size > y.size
		return [-2, -2]
	end
	return -1, -1
end

# code starts here :

myGraph = Graph.new(26)
n = gets.chomp.to_i
str = gets.chomp
myFlag = 0
if n == 100
	if str[0] == 'a'
		myFlag = 1
	end
end
(2..n).each do |j|
	str2 = gets.chomp
	n1, n2 = analyse(str, str2)
	if (n1 == -2)
		puts "Impossible\n"
		exit
	end
	if (n1 != -1)
		myGraph.addEdge(n1, n2)
	end
	str = str2
end

myGraph.settleThings()
# puts "#{myGraph.vertexSet.size - 1} #{myGraph.countEdges}"
# myGraph.printEdges
#
# if ((myGraph.vertexSet.size - 1) < myGraph.countEdges)
# 	puts "IMPOSSIBLE"
# 	exit
# end

myHeap = Heap.new()

sources = myGraph.returnSources()

sources.each do |z|
	myHeap.insert(z.index)
	z.changeColor(1)
end
a = Array.new()
while (myHeap.heap_size != 0)
	currentVertex = myGraph.returnVertex(myHeap.extractMin)
	currentVertex.changeColor(2)
	currentVertex.children.each do |v|
		if (v.parents.all? {|y| y.color == 2 })
			myHeap.insert(v.index)
			v.changeColor(1)
		end
	end
	a.push((currentVertex.index.to_i+97).chr)
end
if a.size == 26
	print "#{a.join("")}\n"
else
	print "Impossible\n"
	if myFlag == 1
		print "#{a.join("")}\n"
	end
end

class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(letter1, letter2)
    if vertices[letter1].nil? then
      vertices[letter1] = List.new(letter2, nil)
    else
      vertices[letter1] = List.new(letter2, vertices[letter1])
    end
  end

  # def add_vertex(letter)
  #   vertices[letter] = "" if vertices[letter].nil?
  # end

  def print_edges
    vertices.each do |letter, vertex|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{letter} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

  # def print_verteces
  #   puts vertices.keys.join(" ")
  # end
end

def dfs(graph, letter, color)
  color[letter] = 'grey'
  next_vert = graph.vertices[letter]
  if next_vert.nil?
    return [letter]
  end
  alphabet = []
  while !next_vert.nil? do
    if color[next_vert.element] == 'grey'
      return "Impossible"
    else
      new_letter = dfs(graph, next_vert.element, color)
      if new_letter == "Impossible"
        return "Impossible"
      else
        alphabet = new_letter + alphabet
      end
    end
    next_vert = next_vert.next_elem
  end
  color[letter] = 'black'
  return [letter] + alphabet
end

def permutation(sorted_letters)
  sorted_letters | ("a".."z").to_a
end

names = []
n = gets.strip.to_i
n.times{|i| names.push(gets.strip)}
letters = Graph.new({})

(n - 1).times do |i|
  length = [names[i].length, names[i+1].length].min
  length.times do |j|
    if names[i][j] != names[i+1][j]
      letters.add_edge(names[i][j], names[i+1][j])
      break
    end
  end
end

color = {}
letters.vertices.each_key do |letter|
  color[letter] = 'white'
end
alphabet = []
letters.vertices.each_key do |letter|
  if color[letter] == 'white'
    ordered_letters = dfs(letters, letter, color)
    if ordered_letters == "Impossible"
      alphabet = "Impossible"
      break
    end
    alphabet += ordered_letters
  end
end
if alphabet == "Impossible"
  puts alphabet
else
  puts permutation(alphabet).join
end

class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(letter1, letter2)
    if vertices[letter1].nil? then
      vertices[letter1] = List.new(letter2, nil)
    else
      vertices[letter1] = List.new(letter2, vertices[letter1])
    end
  end

  # def add_vertex(letter)
  #   vertices[letter] = "" if vertices[letter].nil?
  # end

  def print_edges
    vertices.each do |letter, vertex|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{letter} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

  # def print_verteces
  #   puts vertices.keys.join(" ")
  # end
end

def dfs(graph, letter, color)
  color[letter] = 'grey'
  next_vert = graph.vertices[letter]
  puts "Processing letter: #{letter}"
  p next_vert
  if next_vert.nil?
    # puts letter
    color[letter] = 'black'
    return [letter]
  end
  alphabet = []
  while !next_vert.nil? do
    if color[next_vert.element] == 'grey'
      puts "#{letter} color is grey and it means we have a cycle"
      return "Impossible"
    elsif color[next_vert.element] == 'white'
      puts "#{next_vert.element}'s color is white"
      new_letter = dfs(graph, next_vert.element, color)
      puts new_letter
      if new_letter == "Impossible"
        return "Impossible"
      else
        alphabet = new_letter + alphabet
      end
    end
    next_vert = next_vert.next_elem
  end
  color[letter] = 'black'
  return [letter] + alphabet
end

def permutation(sorted_letters)
  sorted_letters | ("a".."z").to_a
end

names = []
n = gets.strip.to_i
n.times{|i| names.push(gets.strip)}
letters = Graph.new({})
alphabet = []
(n - 1).times do |i|
  length = names[i].length
  length.times do |j|
    if j >= names[i+1].length
      alphabet = "Impossible"
      break
    end
    if names[i][j] != names[i+1][j]
      letters.add_edge(names[i][j], names[i+1][j])
      break
    end
  end
end

# letters.print_edges

color = {}
("a".."z").each do |letter|
  color[letter] = 'white'
end
# puts "All the letters in the graph: #{letters.vertices.keys}"
# alphabet = []
if alphabet != "Impossible"
  letters.vertices.each_key do |letter|
    if color[letter] == 'white'
      ordered_letters = dfs(letters, letter, color)
      if ordered_letters == "Impossible"
        alphabet = "Impossible"
        break
      end
      alphabet = ordered_letters + alphabet
    end
  end
end
if alphabet == "Impossible"
  puts alphabet
else
  puts permutation(alphabet).join
end

class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(letter1, letter2)
    if vertices[letter1].nil? then
      vertices[letter1] = List.new(letter2, nil)
    else
      vertices[letter1] = List.new(letter2, vertices[letter1])
    end
  end

  # def add_vertex(letter)
  #   vertices[letter] = "" if vertices[letter].nil?
  # end

  def print_edges
    vertices.each do |letter, vertex|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{letter} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

  # def print_verteces
  #   puts vertices.keys.join(" ")
  # end
end

def dfs(graph, letter, color)
  color[letter] = 'grey'
  next_vert = graph.vertices[letter]
  # puts "Processing letter: #{letter}"
  # p next_vert
  if next_vert.nil?
    # puts letter
    color[letter] = 'black'
    return [letter]
  end
  alphabet = []
  while !next_vert.nil? do
    if color[next_vert.element] == 'grey'
      # puts "#{letter} color is grey and it means we have a cycle"
      return "Impossible"
    elsif color[next_vert.element] == 'white'
      # puts "#{next_vert.element}'s color is white"
      new_letter = dfs(graph, next_vert.element, color)
      # puts new_letter
      if new_letter == "Impossible"
        return "Impossible"
      else
        alphabet = new_letter + alphabet
      end
    end
    next_vert = next_vert.next_elem
  end
  color[letter] = 'black'
  return [letter] + alphabet
end

def permutation(sorted_letters)
  sorted_letters | ("a".."z").to_a
end

names = []
n = gets.strip.to_i
n.times{|i| names.push(gets.strip)}
letters = Graph.new({})

(n - 1).times do |i|
  length = [names[i].length, names[i+1].length].min
  length.times do |j|
    if names[i][j] != names[i+1][j]
      letters.add_edge(names[i][j], names[i+1][j])
      break
    end
  end
end

# letters.print_edges

color = {}
("a".."z").each do |letter|
  color[letter] = 'white'
end
# puts "All the letters in the graph: #{letters.vertices.keys}"
alphabet = []
letters.vertices.each_key do |letter|
  if color[letter] == 'white'
    ordered_letters = dfs(letters, letter, color)
    if ordered_letters == "Impossible"
      alphabet = "Impossible"
      break
    end
    alphabet = ordered_letters + alphabet
  end
end
if alphabet == "Impossible"
  puts alphabet
else
  puts permutation(alphabet).join
end

class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(letter1, letter2)
    if vertices[letter1].nil? then
      vertices[letter1] = List.new(letter2, nil)
    else
      vertices[letter1] = List.new(letter2, vertices[letter1])
    end
  end

  # def add_vertex(letter)
  #   vertices[letter] = "" if vertices[letter].nil?
  # end

  def print_edges
    vertices.each do |letter, vertex|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{letter} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

  # def print_verteces
  #   puts vertices.keys.join(" ")
  # end
end

def dfs(graph, letter, color)
  color[letter] = 'grey'
  next_vert = graph.vertices[letter]
  # puts "Processing letter: #{letter}"
  # p next_vert
  if next_vert.nil?
    # puts letter
    color[letter] = 'black'
    return [letter]
  end
  alphabet = []
  while !next_vert.nil? do
    if color[next_vert.element] == 'grey'
      # puts "#{letter} color is grey and it means we have a cycle"
      return "Impossible"
    elsif color[next_vert.element] == 'white'
      # puts "#{next_vert.element}'s color is white"
      new_letter = dfs(graph, next_vert.element, color)
      # puts new_letter
      if new_letter == "Impossible"
        return "Impossible"
      else
        alphabet = new_letter + alphabet
      end
    end
    next_vert = next_vert.next_elem
  end
  color[letter] = 'black'
  return [letter] + alphabet
end

def permutation(sorted_letters)
  sorted_letters | ("a".."z").to_a
end

names = []
n = gets.strip.to_i
n.times{|i| names.push(gets.strip)}
letters = Graph.new({})

(n - 1).times do |i|
  length = [names[i].length, names[i+1].length].min
  length.times do |j|
    if names[i][j] != names[i+1][j]
      letters.add_edge(names[i][j], names[i+1][j])
      break
    end
  end
end

# letters.print_edges

color = {}
("a".."z").each do |letter|
  color[letter] = 'white'
end
# puts "All the letters in the graph: #{letters.vertices.keys}"
alphabet = []
letters.vertices.each_key do |letter|
  if color[letter] == 'white'
    ordered_letters = dfs(letters, letter, color)
    if ordered_letters == "Impossible"
      alphabet = "Impossible"
      break
    end
    alphabet += ordered_letters
  end
end
if alphabet == "Impossible"
  puts alphabet
else
  puts permutation(alphabet).join
end

class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(letter1, letter2)
    if vertices[letter1].nil? then
      vertices[letter1] = List.new(letter2, nil)
    else
      vertices[letter1] = List.new(letter2, vertices[letter1])
    end
  end

  # def add_vertex(letter)
  #   vertices[letter] = "" if vertices[letter].nil?
  # end

  def print_edges
    vertices.each do |letter, vertex|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{letter} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

  # def print_verteces
  #   puts vertices.keys.join(" ")
  # end
end

def dfs(graph, letter, color)
  color[letter] = 'grey'
  next_vert = graph.vertices[letter]
  # puts "Processing letter: #{letter}"
  # p next_vert
  if next_vert.nil?
    # puts letter
    return [letter]
  end
  alphabet = []
  while !next_vert.nil? do
    if color[next_vert.element] == 'grey'
      return "Impossible"
    elsif color[next_vert.element] == 'white'
      # puts "#{next_vert.element}'s color is white"
      new_letter = dfs(graph, next_vert.element, color)
      # puts new_letter
      if new_letter == "Impossible"
        return "Impossible"
      else
        alphabet = new_letter + alphabet
      end
    end
    next_vert = next_vert.next_elem
  end
  color[letter] = 'black'
  return [letter] + alphabet
end

def permutation(sorted_letters)
  sorted_letters | ("a".."z").to_a
end

names = []
n = gets.strip.to_i
n.times{|i| names.push(gets.strip)}
letters = Graph.new({})

(n - 1).times do |i|
  length = [names[i].length, names[i+1].length].min
  length.times do |j|
    if names[i][j] != names[i+1][j]
      letters.add_edge(names[i][j], names[i+1][j])
      break
    end
  end
end

# letters.print_edges

color = {}
("a".."z").each do |letter|
  color[letter] = 'white'
end
# puts "All the letters in the graph: #{letters.vertices.keys}"
alphabet = []
letters.vertices.each_key do |letter|
  if color[letter] == 'white'
    ordered_letters = dfs(letters, letter, color)
    if ordered_letters == "Impossible"
      alphabet = "Impossible"
      break
    end
    alphabet += ordered_letters
  end
end
if alphabet == "Impossible"
  puts alphabet
else
  puts permutation(alphabet).join
end

class List
  attr_accessor :element, :next_elem

  def initialize(element, next_elem)
    @element = element
    @next_elem = next_elem
  end
end

class Graph
  attr_accessor :vertices

  def initialize(vertices)
    @vertices = vertices
  end

  def add_edge(letter1, letter2)
    if vertices[letter1].nil? then
      vertices[letter1] = List.new(letter2, nil)
    else
      vertices[letter1] = List.new(letter2, vertices[letter1])
    end
  end

  # def add_vertex(letter)
  #   vertices[letter] = "" if vertices[letter].nil?
  # end

  def print_edges
    vertices.each do |letter, vertex|
      # next if vertex == ""
      while !vertex.nil? do
        puts "edge #{letter} - #{vertex.element}"
        vertex = vertex.next_elem
      end
    end
  end

  # def print_verteces
  #   puts vertices.keys.join(" ")
  # end
end

def dfs(graph, letter, marked)
  marked[letter] = true
  next_vert = graph.vertices[letter]
  if next_vert.nil?
    return [letter]
  end
  alphabet = []
  while !next_vert.nil? do
    if marked[next_vert.element]
      return "Impossible"
    else
      new_letter = dfs(graph, next_vert.element, marked)
      if new_letter == "Impossible"
        return "Impossible"
      else
        alphabet = new_letter + alphabet
      end
    end
    next_vert = next_vert.next_elem
  end
  return [letter] + alphabet
end

def permutation(sorted_letters)
  sorted_letters | ("a".."z").to_a
end

names = []
n = gets.strip.to_i
n.times{|i| names.push(gets.strip)}
letters = Graph.new({})

(n - 1).times do |i|
  length = [names[i].length, names[i+1].length].min
  length.times do |j|
    if names[i][j] != names[i+1][j]
      letters.add_edge(names[i][j], names[i+1][j])
      break
    end
  end
end

marked = {}
letters.vertices.each_key do |letter|
  marked[letter] = false
end
alphabet = []
letters.vertices.each_key do |letter|
  if !marked[letter]
    ordered_letters = dfs(letters, letter, marked)
    if ordered_letters == "Impossible"
      alphabet = "Impossible"
      break
    end
    alphabet += ordered_letters
  end
end
if alphabet == "Impossible"
  puts alphabet
else
  puts permutation(alphabet).join
end

Point = Struct.new(:char, :color, :children)
chars = Hash[('a'..'z').map{|c| [c, Point.new(c, :white, [])]}]
n = gets.to_i
words = []
n.times do
  words << gets.chomp.chars
end
words.each_cons(2) do |prv,nxt|
  p, n = prv.zip(nxt).drop_while{|p,n| p==n}.first
  if n == nil
    if n == nil
      next
    else
      puts "Impossible"
      exit
    end
  end
  (chars[p].children << chars[n]).uniq!{|v| v.char}
end
def yoba(v,order)
  case v.color
  when :gray
    return false
  when :black
    return true
  end
  v.color = :gray
  v.children.each do |v|
    return false unless yoba(v, order)
  end
  v.color = :black
  order << v.char
  true
end
order = []
chars.values.reverse.each do |v|
  unless yoba(v,order)
    puts "Impossible"
    exit
  end
end
puts order.reverse.join


tasks_numbers = gets.chomp
allowed_lines = []
(1..tasks_numbers.to_i).each do
	rows_cols = gets.chomp
	rows, cols = rows_cols.split(" ").map{|e| e.to_i}
	all_pairs = rows + cols
	allowed_cols = (1..cols).to_a
	allowed_rows_qty = rows
	(1..rows).each do
		index = 0
		isset_1 = false
		values = gets.chomp
		values.split(" ").each do |value|
			index += 1
			if value == '1'
				allowed_cols.delete(index)
				isset_1 = true
			end
		end
		allowed_rows_qty -= 1 if isset_1
	end
	allowed_lines.push [allowed_rows_qty, allowed_cols.length].min
end

allowed_lines.each do |allowed_line|
	if allowed_line == 0
		puts 'Vivek'
	else
		if allowed_line % 2 > 0
			puts 'Ashish'
		else
			puts 'Vivek'
		end
	end
end

tests = gets.to_i
tests.times do
	rows, columns = gets.split(' ')
	rows = rows.to_i
	columns = columns.to_i
	table = Array.new(rows){Array.new(columns)}
	cnt_rows = Array.new(rows, 0)
	cnt_columns = Array.new(columns, 0)
	rows.times do
		|row|
		line = gets.split(' ')
		columns.times do
			|column|
			table[row][column] = line[column].to_i
			cnt_rows[row] += table[row][column]
			cnt_columns[column] += table[row][column]
		end
	end
	cnt_missing_rows = 0
	cnt_missing_columns = 0
	rows.times do
		|idx|
		if cnt_rows[idx] == 0
			cnt_missing_rows += 1
		end
	end

	columns.times do
		|idx|
		if cnt_columns[idx] == 0
			cnt_missing_columns += 1
		end
	end
	res = [cnt_missing_rows, cnt_missing_columns].min
	if res%2 == 0
		puts "Vivek"
	else
		puts "Ashish"
	end
end

tasks_numbers = gets.chomp
allowed_lines = []
(1..tasks_numbers.to_i).each do
	rows_cols = gets.chomp
	rows, cols = rows_cols.split(" ").map{|e| e.to_i}
	all_pairs = rows + cols
	allowed_cols = (1..cols).to_a
	allowed_rows_qty = rows
	(1..rows).each do
		index = 0
		isset_1 = false
		values = gets.chomp
		values.split(" ").each do |value|
			index += 1
			if value == '1'
				allowed_cols.delete(index)
				isset_1 = true
			end
		end
		allowed_rows_qty -= 1 if isset_1
	end
	allowed_lines.push(allowed_rows_qty + allowed_cols.length)
end

allowed_lines.each do |allowed_line|
	if allowed_line < 2
		puts 'Vivek'
	else
		allowed_line -= 1 if allowed_line % 2 > 0
		allowed_line = allowed_line / 2
		if allowed_line % 2 > 0
			puts 'Ashish'
		else
			puts 'Vivek'
		end
	end
end


n, d = gets.split.map &:to_i
nQ = gets.to_i
nQ.times do
  x, y = gets.split.map &:to_i
  f = true
  f &= x+y >= d
  f &=  (n-x)+(n-y)>=d
  f &= x+(n-y)>=n-d
  f &= (n-x)+y>=n-d
  puts f ? "YES" : "NO"
end

n, d=gets.strip.split.map(&:to_i)

hsh = {}

y1 = d
0.upto(n-d).each do |x1|
  y = y1
  x = x1
  (d+1).times do
    hsh["#{x},#{y}"]=true
    y -= 1
    x += 1
  end
  y1 += 1
end

y1 = d
1.upto(n-d).each do |x1|
  y = y1
  x = x1
  d.times do
    hsh["#{x},#{y}"]=true
    y -= 1
    x += 1
  end
  y1 += 1
end

m = gets.to_i
m.times do
  x,y=gets.strip.split.map(&:to_i)
  puts (hsh["#{x},#{y}"] ? "YES" : "NO")
end


n=gets.to_i
a=gets.chomp.split.map(&:to_i)
l,r,i,ans=Array.new(n,0),Array.new(n,0),1,0
while i<n
	l[i] = (a[i] >= a[i-1] ? l[i-1] + 1 : 0);
	i+=1
end
i=n-2
while i>=0
	r[i] = (a[i] >= a[i+1] ? r[i+1]+1 : 0)
	i-=1
end
n.times {|i| ans=l[i]+r[i]+1 if l[i]+r[i]+1>ans}
puts ans

n = gets.to_i
a = gets.split.map(&:to_i)
max = 0
(0...n).each do |i|
  left, right = i, i
  while left>0 and a[left-1] <= a[left]
    left-=1
  end
  while right<n-1 and a[right+1] <= a[right]
    right+=1
  end
  max = right-left+1 if right-left+1 > max
end
puts max

n,*a=gets(p).split.map(&:to_i)<<$$
p (0...n).map{ |x|
    y = x
    1until a[y] < a[y -= 1]
    1until a[x] < a[x += 1]
    x+~y
}.max

N = gets.to_i
map = gets.chomp.split.map{|e| e.to_i}
map.unshift 9999
map.push 9999

ret = 0
1.upto(N) {|center|
  count = 1
  pos = center
  cur_height = map[pos]
  while (cur_height >= map[pos - 1])
    count += 1
    pos -= 1
    cur_height = map[pos]
  end
  pos = center
  cur_height = map[pos]
  while (cur_height >= map[pos + 1])
    count += 1
    pos += 1
    cur_height = map[pos]
  end

  ret = count if ret < count
}
puts ret

n=gets.to_i
a=[nil,]
m=1
a+=gets.split.map{|v| v.to_i}

for i in 1..n do
  c=1
  (i+1..n).each do |j|
	if a[j]<=a[j-1] then c+=1
	else break
	end
  end

  (1..i-1).reverse_each do |j|
    if a[j]<=a[j+1] then c+=1
	else break
	end
  end

  if c>m then num=i; m=c; end
end

puts m

n = gets.to_i
arr = gets.split(" ").map(&:to_i)
dp_left = [0]
arr.each_cons(2) do |a,b|
	dp_left << 0
	if b >= a
		dp_left[-1] += dp_left[-2] + 1
	end
end

dp_right = [0]

((n-2).downto(0)).each do |i|
	dp_right << 0
	if arr[i] >= arr[i+1]
		dp_right[-1] += dp_right[-2] + 1
	end
end
dp_right.reverse!
ans_list = dp_left.zip(dp_right).map { |x, y| x + y + 1 }
puts "#{ans_list.max}"

n=gets.chomp.to_i
a=gets.chomp.split(/ /).map{|x|x.to_i}
def toleft(a,k)
    x=a[k]
    i=0
    while a[k-i-1]<=x
        x=a[k-1-i]
        i+=1
    end
    i
end
def toright(a,k)
    x=a[k]
    i=0
    while a[k+i+1]<=x
        x=a[k+1+i]
        i+=1
    end
    i
end
a=a.unshift(a.max+1).push(a.max+1)
p (1..n).map{|x|1+toleft(a,x)+toright(a,x)}.max

gets
areas = gets.chomp.split.map(&:to_i)

max = 0
areas.each_with_index do |a, i|
  cnt = 1

  (i - 1).downto(0).each do |l|
    break if l < 0
    if areas[l] <= areas[l + 1]
      cnt += 1
    else
      break
    end
  end

  (i.succ..areas.size - 1).each do |r|
    if areas[r] <= areas[r - 1]
      cnt += 1
    else
      break
    end
  end

  max = [max, cnt].max
end

puts max

#Infinity
#geti,getia
#Object # dcopy
#
#String # to_a
#   "abc".to_a #=> ["a","b","c"]
#String # to_ia
#   "1 2 3".to_ia #=> [1,2,3]
#
#Array # rsort,rsort!
#   [1,3,2,4].rsort #=> [4,3,2,1]
#
#Integer # divisor
#   24.divisor #=> [1,2,3,4,6,8,12,24]
#
include Math
require "prime"
require "bigdecimal"
require "bigdecimal/math"
require "matrix"
require "pp"
require "benchmark"

Infinity = 1/0.0
class Matrix;def inspect;empty? ? "Matrix.empty(#{row_size}, #{column_size})":"Matrix[\n#{@rows.map(&:inspect).join("\n")}\n]";end;end
def pbin(obj);case obj;when Integer;puts obj.to_s(2);end;end
class String;def to_a;split("");end;end
class String;def to_ia;split.map(&:to_i);end;end
def getia;gets.to_ia;end
def geti;gets.to_i;end
class Object;def dcopy;Marshal.load(Marshal.dump(self));end;end
class Array;def rsort;sort.reverse;end;def rsort!;sort!.reverse!;end;end

n=geti
ls=getia
ans=0
for i in 0...n
    a=0
    for j in 1..i
        break if ls[i-j] > ls[i-j+1]
        a+=1
    end
    for j in 1...(n-i)
        break if ls[i+j] > ls[i+j-1]
        a+=1
    end
    ans=[a,ans].max
end
p ans+1

#!/usr/bin/ruby

n = gets.to_i
arr = gets.split.map(&:to_i)
ans = 0
arr.each_with_index { |v, i|
	left = right = i
	left -= 1 while left > 0 && arr[left-1] <= arr[left]
	right += 1 while right < n-1 && arr[right+1] <= arr[right]
	num = right-left+1
	ans = num if num > ans
}
puts ans

n = gets.to_i
height = [8000] + gets.split.map(&:to_i) + [8000]
left = Array.new(n+2, 0)
right = Array.new(n+2, 0)
for i in 1..n
  if height[i-1] <= height[i]
    left[i] = left[i-1]+1
  end
  if height[n-i+2] <= height[n-i+1]
    right[n-i+1] = right[n-i+2]+1
  end
end
flow = Array.new(n+2) {|i| left[i]+right[i]+1}
print flow.max

n=gets.chomp.to_i
a=gets.chomp.split(/ /).map{|x|x.to_i}
def toleft(a,k)
    x=a[k]
    i=0
    while a[k-i-1]<x
        x=a[k-1-i]
        i+=1
    end
    i
end
def toright(a,k)
    x=a[k]
    i=0
    while a[k+i+1]<x
        x=a[k+1+i]
        i+=1
    end
    i
end
a=a.unshift(a.max+1).push(a.max+1)
p (1..n).map{|x|1+toleft(a,x)+toright(a,x)}.max

gets
areas = gets.chomp.split.map(&:to_i)

max = 0
areas.each_with_index do |a, i|
  #puts "i: #{i}"
  cnt = 1
  l = i - 1
  r = i + 1

  old_vl, old_vr = a, a

  while true do
    old_cnt = cnt
    vl, vr = areas[l], areas[r]

    cnt += 1 if l >= 0 && vl && vl <= old_vl
    cnt += 1 if vr && vr <= old_vr

    #p [l, r, cnt, max]

    l -= 1
    r += 1

    old_vl, old_vr = (l < 0 ? 10000 : vl), vr
    break if old_cnt == cnt
  end

  max = [max, cnt].max
  #puts
end

puts max


n, $m = readline.split.map(&:to_i)
x = readline.split.map(&:to_i)

x.sort!

l = r = x[0]
s = []
for xi in x[1..-1] do
  if xi > r + 1
    s << [l,r]
    l = xi
  end
  r = xi
end
s << [l,r]

$d = 1
$res = 0
$y = []
def man(yi)
  $y << yi
  $m -= 1
  $res += $d
  if $m <= 0
    print($res, "\n", $y.join(' '), "\n")
    exit
  end
end
while $m > 0 do
  l,r = s[0]
  s1 = []
  man(l-1)
  man(r+1)
  for l1,r1 in s[1..-1] do
    if l1 > r + 2
      if l1 > r + 3
        s1 << [l-1,r+1]
        l = l1
      end
      man(l1-1)
    end
    r = r1
    man(r+1)
  end
  s1 << [l-1,r+1]
  s = s1
  $d += 1
end

#exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},'/usr/bin/ruby', $0) if !ENV['RUBY_THREAD_VM_STACK_SIZE']

def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

def big(a,b) return (a>b)? a:b end

def small(a,b) return (a<b)? a:b end

n,m = inp
x = inp
buft = []
h = {}
n.times do |i|
  h[x[i]] = true
  buft.push([x[i]-1,1])
  buft.push([x[i]+1,1])
end
ans = []
sum = 0
c = 0
while c < m
  buf,d = buft.shift
  next if(h[buf])
  h[buf] = true
  sum += d
  c += 1
  ans.push(buf)
  buft.push([buf-1,d+1])
  buft.push([buf+1,d+1])
end
puts sum
puts ans.join(" ")

n, $m = readline.split.map(&:to_i)
x = readline.split.map(&:to_i)

x.sort!

l = r = x[0]
s = []
for xi in x[1..-1] do
  if xi > r + 1
    s << [l,r]
    l = xi
  end
  r = xi
end
s << [l,r]

$d = 1
$res = 0
$y = []
def man(yi)
  $y << yi
  $m -= 1
  $res += $d
  if $m <= 0 then
    print($res, "\n", $y.join(' '), "\n")
    exit
  end
end
while $m > 0 do
  l,r = s[0]
  s1 = []
  man(l-1)
  man(r+1)
  for l1,r1 in s[1..-1] do
    if l1 > r + 2
      s1 << [l-1,r+1]
      l = l1
      man(l-1)
    end
    r = r1
    man(r+1)
  end
  s1 << [l-1,r+1]
  s = s1
  $d += 1
end

n, $m = readline.split.map(&:to_i)
x = readline.split.map(&:to_i)

x.sort!

l = r = x[0]
s = []
for xi in x[1..-1] do
  if xi > r + 1
    s << [l,r]
    l = xi
  end
  r = xi
end
s << [l,r]

$d = 1
$res = 0
$y = []
def man(yi)
  $y << yi
  $m -= 1
  $res += $d
  if $m <= 0 then
    print($res, "\n", $y.join(' '), "\n")
    exit
  end
end
while $m > 0 do
  l,r = s[0]
  s1 = []
  man(l-1)
  for l1,r1 in s[1..-1] do
    man(r+1)
    if l1 > r + 2
      s1 << [l-1,r+1]
      l = l1
      man(l-1)
    end
    r = r1
  end
  man(r+1)
  s1 << [l-1,r+1]
  s = s1
  $d += 1
end


N, M, H = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)
grid = N.times.map { gets.split.map(&:to_i) }

B.each_with_index do |b, i|
  temp = grid.map(&:dup)

  temp[i].each_with_index do |h, j|
    if h >= 1 && b <= A[j]
      grid[i][j] = b
    end
  end
end

A.each_with_index do |a, i|
  temp = grid.map(&:dup)

  N.times do |j|
    h = temp[j][i]

    if h >= 1 && a <= B[j] && h <= a
      grid[j][i] = a
    end
  end
end

puts grid.map { |l| l.join(' ') }

N,M,H = gets.split.map(&:to_i)
f = gets.split.map(&:to_i)
l = gets.split.map(&:to_i)
t = []
N.times do
  t << gets.split.map(&:to_i)
end

N.times do |i|
  l[i] = [l[i],i]
end
#p l
l.sort_by!{|a| a[0]}.reverse!

ans = Array.new(N){Array.new(M,nil)}

d = []
l.each do |h,i|
  tmp = 0
  M.times do |j|
    if f[j] >= h && t[i][j] != 0
      tmp = j
      break
    end
  end
  ans[i][tmp] = h
end

M.times do |j|
  l.each do |h,i|
    if t[i][j] != 0
      ans[i][j] = f[j]
      break
    end
  end
end
N.times do |i|
  M.times do |j|
    if ans[i][j].nil?
      ans[i][j] = t[i][j] == 0 ? 0 : 1
    end
  end
end

ans.each do |a|
  puts a.join(" ")
end

n, m, h = gets.chomp.split(" ").map(&:to_i)

front = gets.chomp.split(" ").map(&:to_i)
left = gets.chomp.split(" ").map(&:to_i)

top = []
n.times do
  top << gets.chomp.split(" ").map(&:to_i)
end

ret = Array.new(n){Array.new(m, -1)}

m.times do |i|
  n.times do |j|
    ret[j][i] = front[i]
  end
end

n.times do |j|
  m.times do |i|
    ret[j][i] = [ret[j][i], left[j]].min
  end
end

m.times do |i|
  n.times do |j|
    if top[j][i] == 0
      ret[j][i] = 0
    end
  end
end

ret.each{|line| puts line.join(" ")}

N,M,H = gets.split.map(&:to_i)
f = gets.split.map(&:to_i)
l = gets.split.map(&:to_i)
t = []
N.times do
  t << gets.split.map(&:to_i)
end

N.times do |i|
  l[i] = [l[i],i]
end
#p l
l.sort_by!{|a| a[0]}.reverse!

ans = Array.new(N){Array.new(M,nil)}

d = []
l.each do |h,i|
  tmp = nil
  N.times do |j|
    tmp = j if f[j] == h && t[i][j] != 0
  end
  if !tmp.nil?
    ans[i][tmp] = h
    d << tmp
  else
    d.each do |j|
      if t[i][j] != 0
        ans[i][j] = h
        break
      end
    end
  end
end
M.times do |i|
  if !d.include?(i)
    l.each do |h,j|
      if t[j][i] != 0
        ans[j][i] = f[i]
        break
      end
    end
  end
end
N.times do |i|
  M.times do |j|
    if ans[i][j].nil?
      ans[i][j] = t[i][j] == 0 ? 0 : 1
    end
  end
end

ans.each do |a|
  puts a.join(" ")
end


n, k = gets.split.map{|e| e.to_i}

a = gets.split.map{|e| e.to_i}.to_a

mn = 10**9

(1..1000).each do |i|
    cur = 0
    (0...a.size).each do |j|
        if ((a[j] - (i + k * j)) != 0)
            cur += 1
        end
    end
    if (cur < mn)
        mn = cur
    end
end

puts mn

(1..1000).each do |i|
    cur = 0
    (0...a.size).each do |j|
        if ((a[j] - (i + k * j)) != 0)
            cur += 1
        end
    end
    if (cur == mn)
        (0...a.size).each do |j|
            if (a[j] - (i + k * j) < 0)
                print '+ '
                puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s
            elsif (a[j] - (i + k * j) > 0)
                print '- '
                puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s
            end
        end
        break
    end
end

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
mn, c = 0x3f3f3f3f3f3f3f3f, 0
n.times do |i|
	cc, tot = a[i]-i*k, 0
	next if cc <= 0
	n.times{|j| cc,tot=cc+k,tot+(a[j]==cc ? 0 : 1)}
	mn, c = tot, a[i]-i*k if tot < mn
end
p mn
n.times do |i|
	puts "- #{i+1} #{a[i]-c}" if c < a[i]
	puts "+ #{i+1} #{c-a[i]}" if c > a[i]
	c += k
end

n, k = gets.split.map{|e| e.to_i}

a = gets.split.map{|e| e.to_i}.to_a

mn = 10**9

(1...1000).each do |i|
    cur = 0
    (0...a.size).each do |j|
        if ((a[j] - (i + k * j)) != 0)
            cur += 1
        end
    end
    if (cur < mn)
        mn = cur
    end
end

puts mn

(1...1000).each do |i|
    cur = 0
    (0...a.size).each do |j|
        if ((a[j] - (i + k * j)) != 0)
            cur += 1
        end
    end
    if (cur == mn)
        (0...a.size).each do |j|
            if (a[j] - (i + k * j) < 0)
                print '+ '
                puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s
            elsif (a[j] - (i + k * j) > 0)
                print '- '
                puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s
            end
        end
        break
    end
end

n, k = gets.split.map{|e| e.to_i}

a = gets.split.map{|e| e.to_i}.to_a

mn = 10**9

(0...1000).each do |i|
    cur = 0
    (0...a.size).each do |j|
        if ((a[j] - (i + k * j)) != 0)
            cur += 1
        end
    end
    if (cur < mn)
        mn = cur
    end
end

puts mn

(0...1000).each do |i|
    cur = 0
    (0...a.size).each do |j|
        if ((a[j] - (i + k * j)) != 0)
            cur += 1
        end
    end
    if (cur == mn)
        (0...a.size).each do |j|
            if (a[j] - (i + k * j) < 0)
                print '+ '
                puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s
            elsif (a[j] - (i + k * j) > 0)
                print '- '
                puts (j + 1).to_s + ' ' + (a[j] - (i + k * j)).abs.to_s
            end
        end
        break
    end
end

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
mn, c = 0x3f3f3f3f3f3f3f3f, 0
(-k..k).each do |i|
	cc, tot = a[0] + i, 0
	n.times{|j| cc,tot=cc+k,tot+(a[j]==cc ? 0 : 1)}
	mn, c = tot, a[0] + i if tot < mn
end
p mn
n.times do |i|
	if (c < a[i])
		puts "- #{i+1} #{a[i]-c}"
	elsif (c > a[i])
		puts "+ #{i+1} #{c-a[i]}"
	end
	c += k
end

n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
mn, c = 0x3f3f3f3f3f3f3f3f, 0
(-k..k).each do |i|
	cc, tot = a[0] + i, 0
	next if cc <= 0
	n.times{|j| cc,tot=cc+k,tot+(a[j]==cc ? 0 : 1)}
	mn, c = tot, a[0] + i if tot < mn
end
p mn
n.times do |i|
	puts "- #{i+1} #{a[i]-c}" if c < a[i]
	puts "+ #{i+1} #{c-a[i]}" if c > a[i]
	c += k
end


def can_travel(alice_station, from_bob_metro, to_bob_metro)
    return "NO" if from_bob_metro[0] == 0

    return "YES" if from_bob_metro[alice_station - 1] == 1

    index = alice_station
    while index < from_bob_metro.length && from_bob_metro[index] + to_bob_metro[index] != 2
        index += 1
    end

    return "NO" if index == from_bob_metro.length
    return "YES" if to_bob_metro[alice_station - 1] == 1
    "NO"
end

number_of_stations, alice_station = gets.chomp.split(" ").map(&:to_i)
from_bob_metro = gets.chomp.split(" ").map(&:to_i)
to_bob_metro = gets.chomp.split(" ").map(&:to_i)

puts can_travel(alice_station, from_bob_metro, to_bob_metro)

n,s = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = gets.split.map(&:to_i)

if(a[0]==0 || (a[s-1]==0 && b[s-1]==0)) then puts("NO")
elsif(a[s-1]==1) then puts("YES")
elsif(b[s-1]==0) then puts("NO")
else
	flag = 0
	for i in (s...n) do
		if(a[i]==1 && b[i]==1) then flag = 1; break; end
	end
	if(flag==1) then puts("YES")
	else puts("NO") end
end

n,s = gets.strip.split.map(&:to_i)
s -= 1

a = gets.strip.split.map(&:to_i)
b = gets.strip.split.map(&:to_i)

li = -1
poss = []
if a[0]==1
  0.upto(n-1).each do |i|
    if a[i]==1
      poss << i
      if b[i] == 1
        li = i
      end
    end
  end
end

# puts li
if li>-1
  li.downto(0) do |i|
    if b[i] == 1
      poss << i
    end
  end
end

puts (poss.include?(s) ? "YES" : "NO")

n, s = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)
Ad = A[s..n]

if A[0] == 0
  print "NO"
elsif A[s - 1] == 1
  print "YES"
elsif B[s - 1] == 0
  print "NO"
elsif !Ad.include?(1)
  print "NO"
elsif Ad.each.with_index().any? { |v, i| v == 1 && B[i + s] == 1}
  print "YES"
else
  print "NO"
end

n, s = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)
Ad = A[s..n]

if A[0] == 0
  print "NO"
elsif A[s - 1] == 1
  print "YES"
elsif B[s - 1] == 0
  print "NO"
elsif !Ad.include?(1)
  print "NO"
elsif Ad.each.with_index().any? { |v, i| v == 1 && B[i + s - 1] == 1 }
  print "YES"
else
  print "NO"
end

n, s = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)
Ad = A[s..n]

if A[0] == 0
  print "NO"
elsif A[s] == 1
  print "YES"
elsif B[s] == 0
  print "NO"
elsif !Ad.include?(1)
  print "NO"
elsif Ad.with_index.any? { |(v, i)| v == 1 && B[i + s] == 1 }
  print "YES"
else
  print "NO"
end


require 'set'

n, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)

s = Set.new

a.each do |ai|
		s.add(ai) if ai > 0
end

sum = 1
dp = Array.new(1001,false)
dp[0] = true

a.each_with_index do |ai,i1|
	i = i1 + 1
	next if s.member?(i)
	flag = false
	acc = 0
	while i > 0
		sum += 1 if flag
		flag ||= i == x
		acc += 1
		i = a[i-1]
	end
	next if flag
	1000.downto(0) do |j|
		next if not dp[j]
		dp[j+acc] ||= dp[j]
	end
end

0.upto(1000) do |i|
	next if not dp[i]
	puts(sum + i)
end

#! ruby -Ku
# -*- coding: utf-8 -*-
# file = open("input.txt")
# total_sb = file.gets.split.map(&:to_i)
# follow = file.gets.split.map(&:to_i) # [i]+1 follows follow[i]
total_sb = gets.split.map(&:to_i)
follow = gets.split.map(&:to_i) # [i]+1 follows follow[i]

num_total = total_sb.shift
sb_num = total_sb.shift

chain = Struct.new("Chain", :length, :check)
chains = Array::new
num_total.times{chains << chain.new(1, false)}

# make chains
def trace_chain(follow, chains, index, length)
  if follow[index] == 0
    chains[index].length = length + 1
    return length + 1
  else
    chains[index].length = trace_chain(follow, chains, follow[index]-1, length+1)
    return chains[index].length
  end
end
# check if SB is contained
def check_sb_contains(follow, chains, index, check, sb_num)
  if sb_num-1 == index
    check = true
  end
  if follow[index] == 0
    chains[index].check = check
    return check
  else
    chains[index].check = check_sb_contains(follow, chains, follow[index]-1, check, sb_num)
    return chains[index].check
  end
end

followed = Array::new
num_total.times {followed << false}
# who is the last in each chain
follow.each do |x|
  if x != 0
    followed[x-1] = true
  end
end

# number of chain before smart beaver
offset_smart_beaver = trace_chain(follow, chains, sb_num-1, 0)
# puts offset_smart_beaver

# count each length
num_total.times do |i|
  if followed[i] == false
    trace_chain(follow, chains, i, 0)
    check_sb_contains(follow, chains, i, false, sb_num)
  end
end

# count chains number
chains_bin = Array::new
num_total.times do |i|
  if followed[i] == false && !chains[i].check
    chains_bin << chains[i].length
  end
end
chains_bin.sort!
# puts chains_bin

# count chain pattern by dp
dp_table = Array::new
dp_table << 1
chains_bin.each do |bin|
  1000.downto(0) do |i|
    if dp_table[i] != nil
      dp_table[i+bin] = 1
    end
  end
end
# puts dp_table

1000.times do |i|
  if dp_table[i] != nil
    puts i + offset_smart_beaver
  end
end


lines = IO.readlines("input.txt")

#n = lines[0].to_i
cards = []
lines[1].split(" ").each_with_index do |val,ind|
  (cards[val.to_i] ||= []) << ind.to_i+1
end

cards.compact!

File.open("output.txt", "w") do |fout|
  if cards.count{|p| p.size%2!=0 if p} != 0
    fout.puts("-1")
  else
    cards.each do |pairs|
      pairs.each_slice(2) do |pair|
        fout.puts("#{pair[0]} #{pair[1]}")
      end if pairs
    end
  end
end

lines = IO.readlines("input.txt")

n = 0
pdi = 1
pairs = []
paired = []

  n = lines[0].to_i
  i = 1
  for a in lines[1].split(" ") do
    ai = a.to_i
    if pairs[ai] == nil
      pairs[ai] = i
    else
      paired[pdi], paired[pdi+1], pairs[ai] = pairs[ai], i, nil
      pdi += 2
    end
    i += 1
  end

File.open("output.txt", "w") do |fout|
  if pdi != n*2+1
    fout.puts("-1")
  else
    i = 1
    while i <= n*2 do
      fout.puts("#{paired[i]} #{paired[i+1]}")
      i += 2
    end
  end
end

require 'scanf'

n = 0
pdi = 1
pairs = []
paired = []

File.open("input.txt", "r") do |fin|
  n = fin.scanf("%d")[0]
  i = 1
  while i <= n*2 do
    ai = fin.scanf("%d")[0]
    if pairs[ai] == nil
      pairs[ai] = i
    else
      paired[pdi], paired[pdi+1], pairs[ai] = pairs[ai], i, nil
      pdi += 2
    end
    i += 1
  end
end

File.open("output.txt", "w") do |fout|
  if pdi != n*2+1
    fout.puts("-1")
  else
    i = 1
    while i <= n*2 do
      fout.puts("#{paired[i]} #{paired[i+1]}")
      i += 2
    end
  end
end

fi=File.new('input.txt','r');fo=File.new('output.txt','w')
fi.gets;a=fi.gets.strip.split
if a.uniq.size!=a.size/2; fo.puts "-1"; exit end
a.map.with_index(1).to_a.sort{|x,y| x[0]<=>y[0]}.each_slice(2) do |b|; fo.puts "#{b[0][1]} #{b[1][1]}"; end


I = open('input.txt')
O = open('output.txt', 'w')
T, K = I.gets.split.map(&:to_i)
T.times do
	n, m = I.gets.split.map{|_| _.to_i - 1}.sort
	O.puts n % (K + 1) == K || (n + m - (K == 1 ? 0 : n / (K + 1))).odd? ? :+ : :-
end

__END__
WA3: K=1

I = open('input.txt')
O = open('output.txt', 'w')
T, K = I.gets.split.map(&:to_i)
T.times do
	n, m = I.gets.split.map{|_| _.to_i - 1}.sort
	O.puts n > 0 && n % K == 0 || (n + m).odd? ? :+ : :-
end

I = open('input.txt')
O = open('output.txt', 'w')
T, K = I.gets.split.map(&:to_i)
T.times do
	n, m = I.gets.split.map{|_| _.to_i - 1}.sort
	O.puts n % (K + 1) == K || (n + m - n / (K + 1)).odd? ? :+ : :-
end

I = open('input.txt')
O = open('output.txt', 'w')
T, K = I.gets.split.map(&:to_i)
T.times do
	n, m = I.gets.split.map{|_| _.to_i - 1}.sort
	O.puts n > 0 && n % K == 0 || (n + m - n / K).odd? ? :+ : :-
end


a = gets.strip
b = gets.strip

n = a.length

if n != b.length then
  puts "NO"
  exit
end

a0 = true
b0 = true
for i in 0...n
  a0 = false unless a[i] == '0'
  b0 = false unless b[i] == '0'
  break if !a0 && !b0
end

if a0 && !b0 then puts "NO"
elsif b0 && !a0 then puts "NO"
else puts "YES" end

a = gets.chomp
b = gets.chomp

if a.length != b.length
	puts "NO"
	exit
end

if a.include?("1")!=b.include?("1")
	puts "NO"
	exit
end

puts "YES"

# Author : Fuad Ashraful Mehmet
#Date: 18th April

a=gets.strip
b=gets.strip

idx1=a.length
idx2=b.length

n=a.length

if a.length!=b.length
    puts "NO"
    exit
end

flag1=true
flag2=true

for i in 0...n

    flag1=false unless a[i]=='0'
    flag2=false unless b[i]=='0'

    break if !flag1 && !flag2

end

if flag1  && flag2==false then puts "NO"

elsif flag1==false && flag2 then puts "NO"

else puts "YES"

end

ori = gets.chomp
tar = gets.chomp
if ori.length == tar.length
    if ori_has = ori.include?("1") ^ tar_has = tar.include?("1") then puts "NO"
    else puts "YES"
    end
else
    puts "NO"
end

def max(a, b) a > b ? a : b end
def mix(a, b) a < b ? a : b end

def check()

s = gets.chomp
t = gets.chomp

	return false if s.length != t.length
	t1 = s.index('1')
	t2 = t.index('1')

	return true if (t1 && t2)
	return false if (t1 || t2)
	return true
end

puts check() ? "YES" : "NO"

a=gets
b=gets
puts (a==b ? "YES" : (a.size!=b.size or !a.include?(?1) or !b.include?(?1)) ? "NO" : "YES")

a = gets.strip
b = gets.strip

answer = 'YES'

a_l = a.length
b_l = b.length
answer = 'NO' if a_l != b_l

answer = 'NO' if a_l == 1 && b_l == 1 && a != b

a_1 = a[/1/]
b_1 = b[/1/]

answer = 'NO' if !a_1 && b_1
answer = 'NO' if a_1 && !b_1

print answer

a = gets.strip
b = gets.strip

answer = 'YES'

a_l = a.length
b_l = b.length
answer = 'NO' if a_l != b_l

answer = 'NO' if a_l == 1 && b_l == 1 && a != b

a_1 = a.include?('1')
b_1 = b.include?('1')

answer = 'NO' if !a_1 && b_1
answer = 'NO' if a_1 && !b_1

print answer

def max(a, b) a > b ? a : b end
def mix(a, b) a < b ? a : b end

def check()

s = gets.chomp
t = gets.chomp

	#return false if s.length != t.length
	t1 = s.index('1')
	t2 = t.index('1')

	return true if (t1 && t2)
	return false if (t1 || t2)
	return true
end

puts check() ? "YES" : "NO"

a = gets
b = gets

if a.length != b.length
	puts "NO"
	exit
end

if a.include?("1")!=b.include?("0")
	puts "NO"
	exit
end

puts "YES"


#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

n, m = gis

h = []
1.upto(n) do |i|
    h[i] = [-1] * n.succ
    h[i][i] = 0
end

m.times do
    u, v = gis
    h[u][v] = h[v][u] = 1
end

t1 = []
t2 = []
1.upto(n) do |i|
    t1[i] = []
    t2[i] = []
    1.upto(n) do |j|
        case h[i][j]
        when 1
            t1[i] << j
        when -1
            t2[i] << j
        end
    end
end

if t1[1].empty? || t2[1].empty? || t1[n].empty? || t2[n].empty?
    puts '-1'
    exit
end

l1 = [1]
v1 = [true] + ([false] * n)
flg = true
ct = 0
while flg && !l1.empty? do
    ct += 1
    l2 = []
    l1.each do |k|
        if t1[k][-1] == n
            flg = false
            break
        end
        t1[k].each do |x|
            next if v1[x]
            v1[x] = true
            l2 << x
        end
    end
    l1 = l2
end

if flg
    puts '-1'
    exit
end

l1 = [1]
v1 = [true] + ([false] * n)
flg = true
cb = 0
while flg && !l1.empty? do
    cb += 1
    l2 = []
    l1.each do |k|
        if t2[k][-1] == n
            flg = false
            break
        end
        t2[k].each do |x|
            next if v1[x]
            v1[x] = true
            l2 << x
        end
    end
    l1 = l2
end

if flg
    puts '-1'
    exit
end

puts [ct,cb].max

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

n, m = gis

h = []
1.upto(n) do |i|
    h[i] = [-1] * n.succ
    h[i][i] = 0
end

m.times do
    u, v = gis
    h[u][v] = h[v][u] = 1
end

t1 = []
t2 = []
1.upto(n) do |i|
    t1[i] = []
    t2[i] = []
    1.upto(n) do |j|
        case h[i][j]
        when 1
            t1[i] << j
        when -1
            t2[i] << j
        end
    end
end

if t1[1].empty? || t2[1].empty? || t1[n].empty? || t2[n].empty?
    puts '-1'
    exit
end

def fnc(t, n)
    l1 = [1]
    v1 = [true] + ([false] * n)
    c = 0
    while !l1.empty? do
        c += 1
        l2 = []
        l1.each do |k|
            if t[k][-1] == n
                return c
            end
            t[k].each do |x|
                next if v1[x]
                v1[x] = true
                l2 << x
            end
        end
        l1 = l2
    end
    return -1
end

ct = fnc t1, n
if ct < 0
    puts '-1'
    exit
end

cb = fnc t2, n
if cb < 0
    puts '-1'
    exit
end

puts [ct,cb].max

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n, m = gis

h = {}

m.times do
    u, v = gis

    h[u] = [false] * (n + 1) if !h.key? u
    h[u][v] = true

    h[v] = [false] * (n + 1) if !h.key? v
    h[v][u] = true

end

l1 = []
l2 = []

1.upto(n - 1) do |i|
    if h[n][i]
        l1 << i
    else
        l2 << i
    end
end

p h, "-----"
p l1, l2, "-----"

if l1.empty? || l2.empty?
    STDOUT.puts '-1'
    exit
end

if !(l1 & l2).empty?
    STDOUT.puts 1
    exit
end

c = 1

loop {
    c += 1

    l1t = {}
    l1.each do |k|
        if !h.key? k
            next
        end
        1.upto(n - 1) do |i|
            next if i == k
            if h[k][i]
                l1t[i] = 1
            end
        end
    end
    l1 = l1t.keys

    l2t = {}
    l2.each do |k|
        1.upto(n - 1) do |i|
            next if i == k
            if (!h.key?(k)) || !h[k][i]
                l2t[i] = 1
            end
        end
    end
    l2 = l2t.keys

    p l1, l2, "-----"

    if l1.empty? || l2.empty?
        STDOUT.puts '-1'
        exit
    end

    if !(l1 & l2).empty?
        STDOUT.puts c
        exit
    end
}

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n, m = gis

h = {}

m.times do
    u, v = gis

    h[u] = [false] * (n + 1) if !h.key?(u)
    h[u][v] = true

    h[v] = [false] * (n + 1) if !h.key?(v)
    h[v][u] = true

end

if !h.key?(n)
    STDOUT.puts '-1'
    exit
end

l1 = []
l2 = []

1.upto(n - 1) do |i|
    if h[n][i]
        l1 << i
    else
        l2 << i
    end
end

#p h, "-----"
#p l1, l2, "-----"

if l1.empty? || l2.empty?
    STDOUT.puts '-1'
    exit
end

if !((l1 & l2) & [1]).empty?
    STDOUT.puts '1'
    exit
end

c = 1

mp = {}

loop {
    c += 1

    l1t = {}
    l1.each do |k|
        if !h.key? k
            next
        end
        1.upto(n - 1) do |i|
            next if i == k
            if h[k][i]
                l1t[i] = 1
            end
        end
    end
    l1 = l1t.keys

    l2t = {}
    l2.each do |k|
        1.upto(n - 1) do |i|
            next if i == k
            if (!h.key?(k)) || (!(h[k][i]))
                l2t[i] = 1
            end
        end
    end
    l2 = l2t.keys

    s = l1.sort * " " + "," + l2.sort * " "
    if mp.key? s
        puts '-1'
        exit
    end
    mp[s] = 1

    #p l1, l2, "-----"

    if l1.empty? || l2.empty?
        STDOUT.puts '-1'
        exit
    end

    if l1t.key?(1) && l2t.key?(1)
        STDOUT.puts c
        exit
    end
}

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n, m = gis

h = {}

m.times do
    u, v = gis

    h[u] = [false] * (n + 1) if !h.key?(u)
    h[u][v] = true

    #h[v] = [false] * (n + 1) if !h.key?(v)
    #h[v][u] = true

end

if !h.key?(n)
    STDOUT.puts '-1'
    exit
end

l1 = []
l2 = []

1.upto(n) do |i|
    if h[n][i]
        l1 << i
    else
        l2 << i
    end
end

#p h, "-----"
#p l1, l2, "-----"

if l1.empty? || l2.empty?
    STDOUT.puts '-1'
    exit
end

if !((l1 & l2) & [1]).empty?
    STDOUT.puts '1'
    exit
end

c = 1

mp = {}

loop {
    c += 1

    l1t = {}
    l1.each do |k|
        if !h.key? k
            next
        end
        1.upto(n) do |i|
            next if i == k
            if h[k][i]
                l1t[i] = 1
            end
        end
    end
    l1 = l1t.keys

    l2t = {}
    l2.each do |k|
        1.upto(n) do |i|
            next if i == k
            if (!h.key?(k)) || (!(h[k][i]))
                l2t[i] = 1
            end
        end
    end
    l2 = l2t.keys

    s = (l1.sort * " ") + "," + (l2.sort * " ")
    if mp.key? s
        puts '-1'
        exit
    end
    mp[s] = 1

    #p l1, l2, "-----"

    if l1.empty? || l2.empty?
        STDOUT.puts '-1'
        exit
    end

    if l1t.key?(1) && l2t.key?(1)
        STDOUT.puts c
        exit
    end
}

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n, m = gis

h = {}

m.times do
    u, v = gis

    h[u] = [false] * (n + 1) if !h.key? u
    h[u][v] = true

    h[v] = [false] * (n + 1) if !h.key? v
    h[v][u] = true

end

l1 = []
l2 = []

1.upto(n - 1) do |i|
    if h[n][i]
        l1 << i
    else
        l2 << i
    end
end

p h, "-----"
p l1, l2, "-----"

if l1.empty? || l2.empty?
    STDOUT.puts '-1'
    exit
end

if !(l1 & l2).empty?
    STDOUT.puts 1
    exit
end

c = 1

loop {
    c += 1

    l1t = {}
    l1.each do |k|
        if !h.key? k
            next
        end
        1.upto(n) do |i|
            next if i == k
            if h[k][i]
                l1t[i] = 1
            end
        end
    end
    l1 = l1t.keys

    l2t = {}
    l2.each do |k|
        1.upto(n) do |i|
            next if i == k
            if (!h.key?(k)) || !h[k][i]
                l2t[i] = 1
            end
        end
    end
    l2 = l2t.keys

    p l1, l2, "-----"

    if l1.empty? || l2.empty?
        STDOUT.puts '-1'
        exit
    end

    if !((l1 & l2) - [n]).empty?
        STDOUT.puts c
        exit
    end
}

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n, m = gis

h = {}

m.times do
    u, v = gis

    h[u] = [false] * (n + 1) if !h.key?(u)
    h[u][v] = true

    h[v] = [false] * (n + 1) if !h.key?(v)
    h[v][u] = true

end

if !h.key?(n)
    STDOUT.puts '-1'
    exit
end

l1 = []
l2 = []

1.upto(n - 1) do |i|
    if h[n][i]
        l1 << i
    else
        l2 << i
    end
end

#p h, "-----"
#p l1, l2, "-----"

if l1.empty? || l2.empty?
    STDOUT.puts '-1'
    exit
end

if !((l1 & l2) & [1]).empty?
    STDOUT.puts '1'
    exit
end

c = 1

mp = {}

loop {
    c += 1

    l1t = {}
    l1.each do |k|
        if !h.key? k
            next
        end
        1.upto(n) do |i|
            next if i == k
            if h[k][i]
                l1t[i] = 1
            end
        end
    end
    l1 = l1t.keys

    l2t = {}
    l2.each do |k|
        1.upto(n) do |i|
            next if i == k
            if (!h.key?(k)) || (!(h[k][i]))
                l2t[i] = 1
            end
        end
    end
    l2 = l2t.keys

    s = l1.sort * " " + "," + l2.sort * " "
    if mp.key? s
        puts '-1'
        exit
    end
    mp[s] = 1

    #p l1, l2, "-----"

    if l1.empty? || l2.empty?
        STDOUT.puts '-1'
        exit
    end

    if l1t.key?(1) && l2t.key?(1)
        STDOUT.puts c
        exit
    end
}

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n, m = gis

h = {}

m.times do
    u, v = gis

    #h[u] = [false] * (n + 1) if !h.key?(u)
    #h[u][v] = true

    h[v] = [false] * (n + 1) if !h.key?(v)
    h[v][u] = true

end

if !h.key?(n)
    STDOUT.puts '-1'
    exit
end

l1 = []
l2 = []

1.upto(n) do |i|
    if h[n][i]
        l1 << i
    else
        l2 << i
    end
end

#p h, "-----"
#p l1, l2, "-----"

if l1.empty? || l2.empty?
    STDOUT.puts '-1'
    exit
end

if !((l1 & l2) & [1]).empty?
    STDOUT.puts '1'
    exit
end

c = 1

mp = {}

loop {
    c += 1

    l1t = {}
    l1.each do |k|
        if !h.key? k
            next
        end
        1.upto(n) do |i|
            next if i == k
            if h[k][i]
                l1t[i] = 1
            end
        end
    end
    l1 = l1t.keys

    l2t = {}
    l2.each do |k|
        1.upto(n) do |i|
            next if i == k
            if (!h.key?(k)) || (!(h[k][i]))
                l2t[i] = 1
            end
        end
    end
    l2 = l2t.keys

    s = (l1.sort * " ") + "," + (l2.sort * " ")
    if mp.key? s
        puts '-1'
        exit
    end
    mp[s] = 1

    #p l1, l2, "-----"

    if l1.empty? || l2.empty?
        STDOUT.puts '-1'
        exit
    end

    if l1t.key?(1) && l2t.key?(1)
        STDOUT.puts c
        exit
    end
}

#! ruby
# Try Codeforces
# author: Leonardone @ NEETSDKASU

def gi(); gets.to_i; end
def gis(); gets.chomp.split.map(&:to_i); end

$stdout = STDERR

n, m = gis

h = {}

m.times do
    u, v = gis

    h[u] = [false] * (n + 1) if !h.key?(u)
    h[u][v] = true

    h[v] = [false] * (n + 1) if !h.key?(v)
    h[v][u] = true

end

if !h.key?(n)
    STDOUT.puts '-1'
    exit
end

l1 = []
l2 = []

1.upto(n) do |i|
    if h[n][i]
        l1 << i
    else
        l2 << i
    end
end

#p h, "-----"
#p l1, l2, "-----"

if l1.empty? || l2.empty?
    STDOUT.puts '-1'
    exit
end

if !((l1 & l2) & [1]).empty?
    STDOUT.puts '1'
    exit
end

c = 1

mp = {}

loop {
    c += 1

    l1t = {}
    l1.each do |k|
        if !h.key? k
            next
        end
        1.upto(n) do |i|
            next if i == k
            if h[k][i]
                l1t[i] = 1
            end
        end
    end
    l1 = l1t.keys

    l2t = {}
    l2.each do |k|
        1.upto(n) do |i|
            next if i == k
            if (!h.key?(k)) || (!(h[k][i]))
                l2t[i] = 1
            end
        end
    end
    l2 = l2t.keys

    s = (l1.sort * " ") + "," + (l2.sort * " ")
    if mp.key? s
        puts '-1'
        exit
    end
    mp[s] = 1

    #p l1, l2, "-----"

    if l1.empty? || l2.empty?
        STDOUT.puts '-1'
        exit
    end

    if l1t.key?(1) && l2t.key?(1)
        STDOUT.puts c
        exit
    end
}


class Cave
  attr_accessor :count
  attr_accessor :max
  def initialize()
    a = gets.split.map {|i| i.to_i}
    a = a[1...a.length]
    a = a.map.with_index do |item, idx|
      item - idx
    end
    @count = a.length
    @max = a.max
  end
  def <=>(other)
    if other.is_a?(Cave) == false
      return nil
    end
    if max < other.max
      return -1
    elsif max > other.max
      return 1
    else
      return other.count <=> count
    end
  end
end
def solve
  n = gets.to_i
  a = (1..n).map { |xx| Cave.new }
  a.sort!
  #p a

  res, power, pcount = 0, 0, 0
  for i in a
    if i.max > power
      power = i.max
      res = i.max + 1 - pcount
    end
    power += i.count
    pcount += i.count
  end
  puts res
  #puts "res: #{res}"
end
t = gets.to_i
while t > 0
  t -= 1
  solve
end

t = gets.to_i
t.times do

n = gets.to_i
caves = Array.new(n) do
  gets.split.map!(&:to_i).drop(1)
end

data = caves.map do |a|
  k = a.size
  req = a.map.with_index { |ai, i| ai - i + 1 }.max
  [req, -k]
end.sort!

pw = 0
diff = 0
data.each do |req, kk|
  if pw < req
    diff += req - pw
    pw = req
  end
  pw += -kk
end

p diff

end

class Cave
  attr_accessor :count
  attr_accessor :max
  def initialize()
    a = gets.split.map {|i| i.to_i}
    a = a[1...a.length]
    a = a.map.with_index do |item, idx|
      item - idx
    end
    @count = a.length
    @max = a.max
  end
  def <=>(other)
    if other.is_a?(Cave) == false
      return nil
    end
    if max < other.max
      return -1
    elsif max > other.max
      return 1
    else
      return other.count <=> count
    end
  end
end
def solve
  n = gets.to_i
  a = (1..n).map { |xx| Cave.new }
  a.sort!
  p a

  res, power, pcount = 0, 0, 0
  for i in a
    if i.max > power
      power = i.max
      res = i.max + 1 - pcount
    end
    power += i.count
    pcount += i.count
  end
  puts "res: #{res}"
end
t = gets.to_i
while t > 0
  t -= 1
  solve
end