Under construction

Still being built. Things may change

Artificial Neural Network

Time limit3sMemory limit1024 MB

Summary
Given a network of linear neurons and Q input vectors, compute the network's output for each input.
Level

Medium5 of 10

Topics
Math, Array
Solved
No attempts yet

Problem

In 2020, the Seoul Metropolitan Office of Education designated Sunlin Internet High School as a high school specializing in artificial intelligence.

Jeong-hwi decided to write neural network problems to check whether his juniors had received good AI education over the past two years.

An artificial neural network is a net-shaped structure made of several artificial neurons. The ii-th artificial neuron has weights Wi,1,Wi,2,⋯ ,Wi,CiW_{i,1}, W_{i,2}, \cdots, W_{i,C_i} and a bias BiB_i. When it receives CiC_i inputs Xi,1,Xi,2,⋯ ,Xi,CiX_{i,1}, X_{i,2}, \cdots, X_{i,C_i}, it outputs the following value.

Yi=∑k=1CiXi,kWi,k+BiY_i = \sum_{k=1}^{C_i} X_{i,k}W_{i,k} + B_i

The figure below shows the structure of an artificial neuron.

For example, take a neuron with weights {3,1}\{3, 1\} and bias 22. If the inputs are {1,2}\{1, 2\}, the output is 1×3+2×1+2=71\times3+2\times1+2=7.

The neural network Jeong-hwi prepared has an input layer, a hidden layer, and an output layer.

The input layer consists of NN inputs A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, and the hidden layer consists of MM artificial neurons. The ii-th hidden neuron takes APi,1,APi,2,⋯ ,APi,CiA_{P_{i,1}}, A_{P_{i,2}}, \cdots, A_{P_{i,C_i}} as inputs and computes its output. The output layer is a single artificial neuron. It takes the outputs of hidden neurons 1,2,⋯ ,M1, 2, \cdots, M in that order as inputs and computes its output. For convenience, the output layer neuron is called the M+1M+1-th artificial neuron.

For example, suppose the weights are W={{1,2},{3,4},{5,6},{10,20,30}}W = \{\{1,2\}, \{3,4\}, \{5,6\}, \{10,20,30\}\}, the biases are B={7,8,9,100}B = \{7, 8, 9, 100\}, and the hidden neurons take the input layer values at positions P={{1,2},{2,3},{4,5}}P = \{\{1, 2\}, \{2, 3\}, \{4, 5\}\} as inputs. If the input is A={2,7,6,8,4}A = \{2, 7, 6, 8, 4\}, the network can be drawn as in the figure below.

Given the structure of a neural network and its input data, write a program that computes the output of the network.

Input

The first line contains three integers NN, MM, and QQ, separated by spaces. NN is the size of the input layer, MM is the number of hidden neurons, and QQ is the number of times the output must be computed. (1≤N,M,Q≤2 000)(1 \leq N,M,Q \leq 2\,000)

From the second line through the MM-th line, each line gives one hidden neuron in order. Each line contains the number of inputs CiC_i, the input positions Pi,1,Pi,2,⋯ ,Pi,CiP_{i,1}, P_{i,2}, \cdots, P_{i,C_i}, the weights Wi,1,Wi,2,⋯ ,Wi,CiW_{i,1}, W_{i,2}, \cdots, W_{i,C_i}, and the bias BiB_i, separated by spaces. (1≤Ci≤N1 \leq C_i \leq N, 1≤Pi,j≤N1 \leq P_{i,j} \leq N, and the Pi,jP_{i,j} are distinct for each ii)

The next line gives the output neuron. It contains the weights WM+1,1,WM+1,2,⋯ ,WM+1,MW_{M+1,1}, W_{M+1,2}, \cdots, W_{M+1,M} and the bias BM+1B_{M+1}, separated by spaces. (1≤Wi,j,Bi≤1001 \leq W_{i,j}, B_i \leq 100)

Each of the next QQ lines contains one set of input data, which is NN integers A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N. (1≤Ai≤1001 \leq A_i \leq 100)

All input numbers are integers.

Output

Print QQ lines. Each line contains the answer for one input.

Hints

Python users are recommended to submit with PyPy.

The answer can exceed the range of a 32-bit integer.

Examples1

  1. Example 1

    Input
    5 3 2
    2 1 2 1 2 7
    2 2 3 3 4 8
    2 4 5 5 6 9
    10 20 30 100
    2 7 6 8 4
    1 2 3 4 5
    
    Expected output
    3580
    2510