Tarot Sham Boast

Given up to 10 equal-length strings over {R,P,S} and a length n random string, sort the strings by the probability each occurs as a contiguous block.

Hard9String matchingProbabilityCombinatoricsMathNo attempts yetTime limit2sMemory limit512 MB

Problem

Every year you reach the final of the rock paper scissors tournament, and every year the same rival beats you. His moves look completely random, yet he tells the press that nobody can beat him.

This year, just before the tournament, you caught him visiting shamans all over town. So you went and visited a whole row of fortune tellers yourself. Each of them laid out a Tarot deck and handed you one sequence of moves that your rival will play at some point during the match.

The predictions are probably worthless. You paid for them anyway, so you may as well watch for some of them during the match. Which ones should you watch?

The final match runs for nn rounds. In each round your rival picks Rock, Paper, or Scissors uniformly at random, independently of every other round. A prediction appears during the match if it occurs as a contiguous block of the moves your rival picked. Sort the predictions by the probability that they appear, from most likely to least likely.

Input

The first line contains two integers nn (1n1061 \le n \le 10^6), the number of rounds in the final match, and ss (1s101 \le s \le 10), the number of predictions.

Each of the next ss lines contains one prediction, a string over the characters R (Rock), P (Paper), and S (Scissors). All predictions have the same length. That length is at least 1, at most nn, and at most 10510^5.

Output

Print the ss predictions, one per line, sorted by decreasing probability that the prediction appears during the match. Print predictions of equal probability in the order they were given in the input.