This page is still under construction.

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

Insufficient Disk Space

Time limit1sMemory limit128 MB

Summary
Given files to delete and files to keep, find the minimum number of rm commands (exact or prefix wildcard) to remove all deletable files while keeping the rest.
Level

Medium7 of 10

Topics
Trie, Greedy, Sorting
Solved
No attempts yet

Problem

Yeonsu is running out of hard-disk space, so he wants to delete unnecessary files to free up room.

All files live in a single directory, and Yeonsu deletes them from that directory using the Linux rm command. The rm command can be used in the following two forms:

  • rm filename — deletes exactly the single file whose name is filename.
  • rm string* — deletes, in one shot, every file whose name equals string or begins with string. The wildcard * may appear only at the very end of the string. (For example, you cannot write something like rm *.txt.) If string is empty, rm * deletes every file in the directory.

You are given a set of files that must be deleted and a set of files that must never be deleted. Find the minimum number of rm commands needed to delete all of the files that must be deleted while deleting none of the files that must not be deleted.

Input

The first line contains the number of test cases T. Each test case has the following format:

  • The first line contains N1, the number of files that must be deleted (1 ≤ N1 ≤ 1000).
  • The next N1 lines each contain one name of a file that must be deleted.
  • The next line contains N2, the number of files that must not be deleted (0 ≤ N2 ≤ 1000).
  • The next N2 lines each contain one name of a file that must not be deleted.

Every file name has length between 1 and 20 and consists only of uppercase and lowercase English letters, digits, and dots (.). Within a single test case, all file names are distinct.

Output

For each test case, print the minimum required number of rm commands on its own line.

Hint

In the first test case, the files that must be deleted can be removed exactly using the following 8 commands:

rm BAPC.*, rm filt*, rm filename*, rm clean, rm cleanup.IN1, rm cleanup.IN2, rm cleanup.out, rm p*

Examples1

  1. Example 1

    Input
    1
    11
    BAPC.in
    BAPC.out
    BAPC.tex
    filter
    filename
    filenames
    clean
    cleanup.IN1
    cleanup.IN2
    cleanup.out
    problem.tex
    5
    BAPC
    files
    cleanup
    cleanup.IN
    cleaning
    
    Expected output
    8