13241 - Co-Sequence   

Description

A Co-Sequence is a sequence which consists of 8 integers.

And you can execute three types of operations on a Co-Sequence:

  1. cyclically shift all the integers to the right in Co-Sequence by one unit
  2. exchange the position of arbitrary two adjacent integers in Co-Sequence
  3. sort some consecutive integers in increasing order in Co-Sequence. Specially, you can execute this type of operation on a Co-Sequence at most once

Now you are given two Co-Sequence  and . You need to answer the minimum number of operations you need to execute on  to make  be as same as . Note it may be impossible to make  be as same as .

You need to solve  tasks.

Input

The first line contains an integer  – the number of tasks you need to solve.

The first line of each task contains 8 integers – the 8 integers of .

The second line of each task contains 8 integers – the 8 integers of  .

All the integers in Co-Sequence must be non-negative and be less than or equal to .

 

It's guaranteed that:

  • The 1st testcase must be identical to the Sample #1 below
  • For the first 2 testcase: only the first type operation is necessary
  • For the first 4 testcase: only the first and the second type operations are necessary
  • For the first 5 testcase: if the third type operations is necessary, then you must execute the third type operation to sort the whole sequence

Output

For each task, output the minimum number of operations you need to execute on  to make  is as same as .

If it's impossible to make  be as same as , output -1 instead.

 

Note: there are two sample below. "# Sample Input 1/2/3" and "# Sample Output 1/2/3" are not the part of input and output.

They are just for marking that the following content corresponds to which sample.

Sample Input  Download

Sample Output  Download

Tags




Discuss