This page is still under construction.

Parts of this page are still being built. What you see may change.

Song

Time limit1sMemory limit256 MB

Summary
Given a 26 by 26 pair score table, pick an L-note song starting from C that maximizes the sum of adjacent pair scores.
Level

Medium7 of 10

Topics
Dynamic programming, Matrix, Graph
Solved
No attempts yet

Problem

A rabbit instrument has 26 notes, written with the uppercase English letters A to Z. Writing these letters in a row gives a rabbit song, and one letter means playing that note for one beat. The length of a song is the number of letters. For example, AASDFG is a song of length 6: beat 1 plays A, beat 2 plays A, beat 3 plays S, beat 4 plays D, beat 5 plays F, and beat 6 plays G.

A rabbit psychologist found that the happiness of the note on beat i+1i+1 is decided by the pair made of the note on beat ii and the note on beat i+1i+1. The values sit in a note happiness table, and the table differs from rabbit to rabbit. A row of the table is the note on beat ii and a column is the note on beat i+1i+1, both in order from A to Z. Write s(x,y)s(x, y) for the value in row xx and column yy. The note on beat 1 gives no happiness, so the song AASDFG gives happiness s(A,A)+s(A,S)+s(S,D)+s(D,F)+s(F,G)s(A, A) + s(A, S) + s(S, D) + s(D, F) + s(F, G).

You are given a note happiness table, the first note of a song, and the length of the song. Find the largest happiness such a song can give.

Input

The first line has the number of test cases TT (1≤T≤101 \le T \le 10). Each test case is made of the following.

  1. The note happiness table, given on 26 lines. Each line has 26 happiness values sijs_{ij} separated by spaces (0≤sij≤1000 \le s_{ij} \le 100). The jj-th value on the ii-th line is the happiness of playing the jj-th note right after the ii-th note, and the notes run from A to Z.
  2. The next line has the number of questions NN (1≤N≤101 \le N \le 10).
  3. Each of the next NN lines has an uppercase English letter CC, the first note of the song, and the length LL of the song, separated by a space (1≤L≤1000001 \le L \le 100000).

Output

For each question, print the largest happiness of a song of length LL that starts with the note CC. Print one value per line, in the order the questions are given.

Examples3

  1. Example 1

    Input
    1
    9 9 8 7 7 0 3 1 6 2 9 7 0 0 0 5 3 2 6 5 4 1 4 7 8 0
    6 2 9 5 0 4 8 3 8 2 8 1 9 7 0 6 3 0 2 1 0 5 0 7 9 3
    9 7 5 9 9 6 2 1 5 6 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 3 4 3 2 8 9 3
    8 9 9 1 6 3 5 9 4 9 3 7 3 8 0 4 1 0 9 0 0 5 5 8 3 6
    8 5 8 6 8 5 4 1 1 0 4 0 8 6 7 7 1 7 9 9 6 6 7 5 5 2
    3 0 0 0 7 5 5 4 0 3 2 0 9 6 6 1 4 2 4 5 3 6 2 7 9 7
    9 0 1 6 1 1 7 8 1 0 1 6 1 5 2 6 8 1 3 3 0 6 6 4 2 4
    2
    A 5
    G 10
    
    Expected output
    36
    81
    
  2. Example 2

    Input
    1
    9 9 8 7 7 0 3 1 6 2 9 7 0 0 0 5 3 2 6 5 4 1 4 7 8 0
    6 2 9 5 0 4 8 3 8 2 8 1 9 7 0 6 3 0 2 1 0 5 0 7 9 3
    9 7 5 9 9 6 2 1 5 6 3 7 9 1 8 8 7 0 8 1 8 8 1 0 1 8
    1 6 9 1 5 0 7 8 0 2 0 9 8 0 2 0 0 1 8 8 4 8 6 3 4 2
    4 6 0 5 6 5 9 7 7 1 8 4 6 3 2 3 8 3 2 4 8 8 0 2 4 3
    2 8 9 4 8 0 9 5 4 2 7 9 3 4 3 7 5 5 1 5 2 4 0 6 1 1
    0 2 8 7 6 9 6 5 8 0 7 6 8 6 1 7 3 0 3 5 9 0 6 5 0 7
    5 6 6 2 8 8 6 7 4 6 0 2 8 2 5 3 4 9 6 2 1 8 7 4 4 9
    1 3 0 1 8 2 2 2 5 1 4 3 6 9 4 9 0 3 0 2 1 5 2 5 5 8
    5 2 5 0 3 2 0 0 9 7 4 0 8 5 6 0 2 6 9 0 0 6 0 2 8 7
    7 2 8 7 8 4 5 0 9 4 0 2 6 3 6 0 2 7 3 9 9 4 8 7 7 9
    6 9 7 2 0 8 3 4 2 9 3 4 1 8 7 1 1 3 4 1 3 9 0 6 4 9
    9 6 7 6 1 7 1 7 2 2 5 0 4 8 5 6 8 0 4 0 0 1 6 8 4 5
    1 0 7 6 9 0 3 6 2 5 0 4 2 8 3 6 4 3 0 9 3 4 2 9 8 5
    0 4 9 9 3 2 1 6 6 9 2 4 9 6 1 2 5 6 0 8 4 2 0 3 6 9
    6 9 8 5 3 9 9 0 7 1 8 8 1 9 4 0 2 9 1 3 1 4 0 7 3 8
    0 7 4 9 3 4 5 4 1 9 7 1 7 2 6 4 1 7 7 2 5 6 0 9 2 9
    0 9 4 9 5 0 1 7 7 7 8 7 0 6 9 6 8 6 7 5 3 1 7 9 7 0
    0 6 1 2 7 2 6 7 3 7 6 2 7 5 3 3 2 1 0 3 2 6 2 8 6 8
    6 1 8 1 4 0 5 6 5 8 1 0 6 5 8 9 1 4 8 6 3 4 5 1 2 4
    4 9 9 5 4 3 8 6 0 6 1 0 4 7 4 5 4 9 8 3 0 5 2 7 6 0
    2 4 2 1 7 1 3 9 4 7 0 5 5 1 9 1 3 0 4 3 4 3 2 8 9 3
    8 9 9 1 6 3 5 9 4 9 3 7 3 8 0 4 1 0 9 0 0 5 5 8 3 6
    8 5 8 6 8 5 4 1 1 0 4 0 8 6 7 7 1 7 9 9 6 6 7 5 5 2
    3 0 0 0 7 5 5 4 0 3 2 0 9 6 6 1 4 2 4 5 3 6 2 7 9 7
    9 0 1 6 1 1 7 8 1 0 1 6 1 5 2 6 8 1 3 3 0 6 6 4 2 4
    5
    A 1
    A 2
    S 6
    Z 3
    D 7
    
    Expected output
    0
    9
    44
    18
    54
    
  3. Example 3

    Input
    1
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0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