# Chefina and Dishes SOLUTIONS CHEFPRTY

Page Contents

### Chefina and Dishes SOLUTIONS CHEFPRTY

There are N cooks (numbered 1 through N) and M dishes (numbered 1 through M). For each legitimate I, the I-th gourmet expert can cook dishes of precisely one sort Fi. Additionally, every gourmet specialist is a companion of K various cooks.

At whatever point some culinary expert cooks a dish, he asks all gourmet specialists who either are his companions or ability to cook this dish to assess the dish. No gourmet specialist may assess his own dish.

All the culinary experts are masterminded in succession from cook 1 to gourmet specialist N. Chefina will pick two substantial numbers L and R and permit the gourmet specialists L,L+1,… ,R to cook their dishes and have them assessed as portrayed previously. She needs to pick L and R so that every one of the N gourmet specialists assesses at any rate one dish. Locate the quantity of such combines.

Info

The main line of the info contains a solitary whole number T indicating the quantity of experiments. The portrayal of T experiments follows.

The primary line of each experiment contains three space-isolated numbers N, M and K.

The subsequent line contains N space-isolated whole numbers F1,F2,… ,FN.

N lines follow. For every I (1≤i≤N), the I-th of these lines contains K space-isolated numbers D1,D2,… ,DK meaning the companions of the I-th gourmet specialist.

Yield

For each experiment, print a solitary line containing one number ― the quantity of sets with the end goal that every gourmet specialist assesses at any rate one dish.

Imperatives

1≤T≤1,000

1≤M≤N≤3⋅105

1≤K≤3

1≤Fi≤M for each substantial I

1≤Di≤N for each substantial I

I !=Di for each legitimate I

D1,D2,… ,DK are pairwise disticnt

the whole of N⋅(K+1) over all experiments doesn’t surpass 2⋅106

Model Input

3 1

1 2 3

6 2 1

1 2

Model Output

9