Under construction

Still being built. Things may change

DNA Copy

Time limit1sMemory limit128 MB

Summary
Given a source string S of length at most 18, find the minimum number of copy operations (each taking a contiguous substring of S or of the already-built target, optionally reversed) needed to assemble T.
Level

Hard8 of 10

Topics
Dynamic programming, Bit manipulation, String, Brute force
Solved
No attempts yet

Problem

Sang-geun decided to simulate DNA copying for his graduation project.

A DNA string consists only of the letters A, C, G, and T. Given a source string SS, find the minimum number of copy operations needed to build a target string TT.

Each copy operation follows these rules:

  • The copied piece must be a contiguous part of SS, or a contiguous part of TT that has already been built.
  • The piece may be reversed before it is placed.
  • Each copied piece fills one contiguous segment of TT; once every segment is filled, TT is complete.

In other words, every position of the finished TT is filled by exactly one copy, taken either from SS or from an already-built part (reversal allowed). The order of filling is free, but a part used as a source must already be complete at the moment it is copied.

For example, with S=S = ACTG and T=T = GTACAATTAAT, TT can be built in 55 copies:

  1. Copy TG from SS and reverse it to GT → GT.........
  2. Copy AC from SS → GTAC.......
  3. Copy TA, a part already built → GTAC...TA..
  4. Copy TA and reverse it to AT → GTAC...TAAT
  5. Copy AAT, a part already built → GTACAATTAAT

Input

The first line contains the number of test cases tt (1≤t≤1001 \le t \le 100). Each test case consists of two lines: the first line contains the string SS and the second line contains the string TT. Both strings consist only of the letters A, C, G, and T, and their lengths are between 11 and 1818, inclusive.

Output

For each test case, print on its own line the minimum number of copies needed to build TT. If it is impossible, print impossible.

Examples4

  1. Example 1

    Input
    5
    ACGT
    GTAC
    A
    C
    ACGT
    TGCA
    ACGT
    TCGATCGA
    A
    AAAAAAAAAAAAAAAAAA
    
    Expected output
    2
    impossible
    1
    4
    6
    
  2. Example 2

    Input
    1
    ACGT
    ACGT
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    AC
    CA
    
    Expected output
    1
    
  4. Example 4

    Input
    1
    A
    AA
    
    Expected output
    2