This page is still under construction.

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

Quantum

Time limit1sMemory limit128 MB

Summary
Given up to 32 quantum operations on length-L bit words and their costs, find the minimum cost to transform each source word into a target word, or report it impossible.
Level

Hard8 of 10

Topics
Graph, Shortest path, Bit manipulation, Implementation
Solved
No attempts yet

Problem

A research team is developing a new way of storing and manipulating data on magnetic disks. The method lets quantum operations act on the sectors of a disk. Each quantum operation costs a certain amount of energy, and the more energy the storage unit consumes, the warmer it gets. Given a set of available quantum operations and their costs, write a program that computes the lowest possible total cost of transforming a given binary word into a desired binary word.

A binary word has length 1≤L≤201 \le L \le 20. Every quantum operation is a string of the same length LL, built from the four letters:

  • N — does nothing (leaves the bit unchanged);
  • F — flips (inverts) the bit;
  • S — sets the bit to 1;
  • C — resets the bit to 0.

The ii-th letter of an operation acts on the bit at position ii of the binary word. Applying an operation transforms every position of the word simultaneously according to its letters. You may apply operations in any order and any number of times. A word is transformed by applying a sequence of operations to it, and the total energy cost equals the sum of the costs of the operations performed. For each requested pair of words, find the minimum total cost to turn the first word into the second, or report that it is impossible.

Input

The first line contains a single integer NN (1≤N≤201 \le N \le 20), the number of test cases. Each test case is given as follows:

  • One line with three integers LL, nop\mathit{nop} and nw\mathit{nw} separated by single spaces, where LL (1≤L≤201 \le L \le 20) is the length of the binary words and operations, nop\mathit{nop} (nop≤32\mathit{nop} \le 32) is the number of available quantum operations, and nw\mathit{nw} (nw≤20\mathit{nw} \le 20) is the number of binary words to transform.
  • Then nop\mathit{nop} lines follow, each containing the definition of one quantum operation (a string of length LL over N, F, S, C) and its energy cost cic_i (0≤ci≤10000 \le c_i \le 1000), separated by a single space.
  • Then nw\mathit{nw} lines follow, each containing two binary words of length LL separated by a single space. The first word must, when possible, be transformed into the second using the available operations. Binary words are written as sequences of 0s and 1s.

Output

For each test case, print one line listing the minimum energy cost of transforming each binary word, in the same order as the input, separated by single spaces. If a binary word cannot be transformed into its target, print NP (not possible) in its place instead of a cost.

Examples1

  1. Example 1

    Input
    2
    4 3 3
    NFFN 1
    NFNF 2
    NNFN 4
    0010 0100
    0001 0010
    0100 1000
    4 4 5
    CFSF 4
    NNSS 3
    FFFF 5
    FNFN 6
    1111 0000
    1001 0110
    0101 1000
    1000 0011
    0000 1001
    
    Expected output
    1 3 NP
    5 4 8 9 9