SETI

Time limit1sMemory limit128 MB

Summary
Recover the coefficients a_0..a_{n-1} of a polynomial over a prime field from its values at k = 1..n.
Level

Medium7 of 10

Topics
Math, Number theory, Implementation
Solved
No attempts yet

Problem

For years, scientists have listened to radio signals from space, hoping to understand what distant civilizations might be trying to say. One especially interesting source is a faraway nebula.

Researchers found that if each message is transmitted as a sequence of integers a0,a1,…,an−1a_0, a_1, \ldots, a_{n-1}, then, when the correct value of pp is used, the function

f(k)=∑i=0n−1aiki(modp),1≤k≤nf(k) = \sum_{i=0}^{n-1} a_i k^i \pmod{p}, \quad 1 \le k \le n

always evaluates to a value in the range 0≤f(k)≤260 \le f(k) \le 26. Here nn is the length of the message, and each coefficient satisfies 0≤ai<p0 \le a_i < p. The number pp is a prime that is guaranteed to be larger than both nn and 2626, and never exceeds 3000030000.

Linguists transcribe each message into a string over the English alphabet. The rule maps the values 1..261..26 that f(k)f(k) may take to the letters a..z (that is, 1=a1 = a, 2=b2 = b, …\ldots, 26=z26 = z); the value 00 is written as an asterisk *. Looping from k=1k = 1 to nn, they append the character corresponding to f(k)f(k) to the string.

Your task is to perform the reverse transcription: given the string and the value of pp that was used, recover the corresponding integer sequence a0,a1,…,an−1a_0, a_1, \ldots, a_{n-1}.

Input

The first line contains a single positive integer NN, the number of test cases that follow. Each case is a single line containing the value of pp used for the transcription, followed by the string to be transcribed, separated by one space. The only characters allowed in the string are the lowercase letters a..z and the asterisk *, and no string is longer than 70 characters.

Output

For each string, output one line with the recovered list of integers, separated by single spaces, given in ascending order of the index ii (from a0a_0 to an−1a_{n-1}).

Examples1

  1. Example 1

    Input
    3
    31 aaa
    37 abc
    29 hello*earth
    
    Expected output
    1 0 0
    0 1 0
    8 13 9 13 4 27 18 10 12 24 15