This page is still under construction.

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

Suspicious Orders

Time limit1sMemory limit128 MB

Summary
Count the cliques in a network of up to 20 people whose combined ordered items cover at least one of the given attack combinations.
Level

Medium6 of 10

Topics
Backtracking, Bit manipulation, Graph
Solved
No attempts yet

Problem

A family once got an unexpected visit from the FBI. The wife had been searching for pressure cookers on the family computer, and the husband had been looking at backpacks. It was right after the Boston Marathon bombing, where the explosives were pressure cookers hidden in backpacks.

Someone who wants to avoid detection does not search for or order every material of an attack himself. A group can split the orders among its members instead, so a record of who knows whom helps in spotting that pattern.

You are given every social connection in a network, and for each person the list of items that person ordered or looked at online. A clique is a set of people in which every two members are connected to each other. You are also given lists of items that together can be used for an attack. Say items {1,4,6}\{1, 4, 6\} together are enough to start an attack. Then a clique whose orders add up to {1,2,4,6,9}\{1, 2, 4, 6, 9\} can carry out an attack as well. Count how many cliques in the data could carry out an attack with what they ordered.

One person may already have ordered all the materials alone. Every clique containing that person then has all the materials too, so many cliques can be counted for the same materials. That is correct.

Input

The first line contains the number KK of data sets, followed by the KK data sets, each of the following form.

The first line of a data set contains four integers nn, mm, kk, cc, separated by spaces. 1≤n≤201 \le n \le 20 is the number of people in the network, 1≤m≤501 \le m \le 50 is the number of items whose purchases are tracked, 0≤k≤1000 \le k \le 100 is the number of item combinations that can be used for an attack, and 0≤c≤4000 \le c \le 400 is the number of connections in the network.

This is followed by kk lines, each describing one combination of materials that can be used for an attack. Each such line contains as its first number an integer sis_i (1≤si≤m1 \le s_i \le m), the number of items this combination contains. This is followed by sis_i integers on the same line, each between 11 and mm.

Next come nn lines describing the items that the corresponding person bought. Line jj starts with a number tjt_j (0≤tj≤m0 \le t_j \le m), the number of items person jj bought. Then follow tjt_j integers between 11 and mm, each giving an item.

Finally, there are cc lines, each containing two distinct integers between 11 and nn, describing a connection between those two people.

Output

For each data set, print Data Set x: on a line by itself, where xx is its number. On the next line, print the number of different cliques of one or more people that could carry out an attack by pooling their materials. Print a blank line between consecutive data sets.

Examples2

  1. Example 1

    Input
    1
    5 4 3 4
    2 1 2
    3 4 1 3
    2 2 4
    1 2
    2 1 4
    1 3
    4 4 2 1 3
    0
    1 2
    1 3
    2 3
    4 5
    
    Expected output
    Data Set 1:
    5
    
  2. Example 2

    Input
    2
    3 2 1 2
    2 1 2
    1 1
    1 2
    0
    1 2
    2 3
    2 3 2 1
    1 3
    2 1 2
    2 1 3
    1 2
    1 2
    
    Expected output
    Data Set 1:
    1
    
    Data Set 2:
    2