Description

A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1S_1 and S2S_2, each stack containing CC chips. Each stack may contain chips of several different colors.
The actual shuffle operation is performed by interleaving a chip from S1S_1 with a chip from S2S_2 as shown below for C=5C=5:

The single resultant stack, S12S_{12}, contains 2C2C chips. The bottommost chip of S12S_{12} is the bottommost chip from S2S_2. On top of that chip, is the bottommost chip from S1S_1. The interleaving process continues taking the 2nd2^{nd} chip from the bottom of S2S_2 and placing that on S12S_{12}, followed by the 2nd chip from the bottom of S1S_1 and so on until the topmost chip from S1S_1 is placed on top of S12S_{12}.
After the shuffle operation, S12S_{12} is split into 22 new stacks by taking the bottommost CC chips from S12S_{12} to form a new S1S_1 and the topmost CC chips from S12S_{12} to form a new S2S_2. The shuffle operation may then be repeated to form a new S12S_{12}.
For this problem, you will write a program to determine if a particular resultant stack S12S_{12} can be formed by shuffling two stacks some number of times.

Input

The first line of input contains a single integer N(1N1000)N (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of four lines of input. The first line of a dataset specifies an integer CC, (1C100)(1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1S_1 and S2S_2). The second line of each dataset specifies the colors of each of the C chips in stack S1S_1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2S_2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2C2C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1S_1 and S2S_2 zero or more times. The bottommost chip’s color is specified first.

Output

Output for each dataset consists of a single line that displays the dataset number (11 though NN), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1(1)1 (−1) for the number of shuffle operations.

Sample Input

2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output

1 2
2 -1

Translation

给定两个长度为lenlen的字符串S1S_1S2S_2, 接着给出一个长度为len×2len\times 2的字符串S12S_{12}
将字符串S1S_1S2S_2通过一定的变换变成S12S_{12},找到最小变换次数。
变换规则如下:
假设S1=AHAH,S2=HAHAS_1=AHAH,S_2=HAHA,变换后的序列 S12=HAAHHAAHS_{12}=HAAHHAAH。类似于洗牌的过程。

Idea

直接暴力模拟洗牌过程,得到目标态就输出变换次数;判断不可能的条件是某一状态前面已经出现过,这样就出现了死循环(用set标记)。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<set>
#include<cstdio>
#include<string>
using namespace std;
int solve()
{
set<string>ex;
int n;
cin>>n;
string s1,s2,s12="";
string res;
cin>>s1>>s2>>res;
int i;
int step=0;
while(true)
{
step++;
for(i=0;i<n;i++) s12=s12+s2[i]+s1[i];
if(ex.find(s12)!=ex.end()) return -1;//出现死循环
else
{
if(s12==res) return step;//得到目标状态
else
{
//将s12分成两半
s1=s12.substr(0,n);
s2=s12.substr(n,n);
ex.insert(s12);
//清空
s12.clear();
}
}
}
}
int main()
{
int t;
cin>>t;
int times=0;
while(++times<=t) printf("%d %d\n",times,solve());
return 0;
}