This page is still under construction.

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

Free Willy

Time limit5sMemory limit256 MB

Summary
Apply at most L of the given position permutations to turn the start word into the target word with the fewest steps.
Level

Medium6 of 10

Topics
BFS, Graph, String
Solved
No attempts yet

Problem

Willy sits in a cage. The guard offers him his freedom if he solves a puzzle.

You are given two words of length NN and PP permutations that Willy may use. A permutation is written as a lowercase string that uses each of the first NN letters of the alphabet exactly once. Applying a permutation qq to a word ww produces a new word whose ii-th letter is the qiq_i-th letter of ww, where qiq_i is the alphabet position of the ii-th letter of qq. Count a as 1 and b as 2.

Applying the permutation bcdefaghi to the word KULTURUKE gives ULTURKUKE. The first six letters rotate one place to the left and the rest stay where they are.

Willy may use only the given permutations, and he may use the same permutation more than once. The cage opens if he turns the first word into the second word with at most LL applications. Find the smallest number of applications.

Input

The first line contains the number of test cases TT. (T≤30T \le 30)

The first line of each test case contains NN, PP, and LL, separated by spaces. (1≤N≤261 \le N \le 26, 1≤P≤101 \le P \le 10, 1≤L≤101 \le L \le 10)

The second line contains two words of length NN, separated by a space. Both words consist of English letters only.

Each of the next PP lines contains one allowed permutation, a string that uses each of the first NN lowercase letters of the alphabet exactly once.

Output

Print one line for each test case. Print the smallest number of applications needed to turn the first word into the second word, or whalemeat if it cannot be done with at most LL applications.

Examples1

  1. Example 1

    Input
    3
    9 6 5
    KULTURUKE UKTURKULE
    bcdefaghi
    cabfdeghi
    bcadefghi
    adcefgbhi
    cgabdefhi
    cdaefhgbi
    9 5 4
    kulturuke tlukuruke
    bcdefaghi
    cabfdeghi
    bcadefghi
    adcefgbhi
    cgabdefhi
    9 3 4
    WILLFREEY FREEWILLY
    bacdefghi
    abghefdic
    fecdbaigh
    
    Expected output
    4
    whalemeat
    4