CF BUDDY
← Problems·

1989B · Substring and Subsequence

1200 · brute force, greedy, strings

Problem: You are given two strings aa and bb, both consisting of lowercase Latin letters.

A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string. A substring of a string is a contiguous subsequence of that string.

For example, consider the string abac:

  • a, b, c, ab, aa, ac, ba, bc, aba, abc, aac, bac and abac are its subsequences;
  • a, b, c, ab, ba, ac, aba, bac and abac are its substrings.

Your task is to calculate the minimum possible length of the string that contains aa as a substring and bb as a subsequence.

Input Format: The first line contains a single integer tt (1t1031 \le t \le 10^3) — the number of test cases.

The first line of each test case contains a string aa (1a1001 \le |a| \le 100), consisting of lowercase Latin letters.

The second line of each test case contains a string bb (1b1001 \le |b| \le 100), consisting of lowercase Latin letters.

Output Format: For each test case, print a single integer — the minimum possible length of the string that contains aa as a substring and bb as a subsequence.

Note: In the examples below, the characters that correspond to the subsequence equal to bb are bolded.

In the first example, one of the possible answers is caba.

In the second example, one of the possible answers is ercf.

In the third example, one of the possible answers is mmm.

In the fourth example, one of the possible answers is contest.

In the fifth example, one of the possible answers is abcdefg.

Sample Cases

Case 1

Input

5
aba
cb
er
cf
mmm
mmm
contest
test
cde
abcefg

Output

4
4
3
7
7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback