Recursive Sequence#4075

Recursive Sequence

1 saniye
64 megabayt
Çok kolay

A sequence of integers a_i is defined as follows:

a_i = b_i \quad \text{for } 1 \leq i \leq k
a_i = c_1 a_{i-1} + c_2 a_{i-2} + \cdots + c_k a_{i-k}, \quad \text{for } i > k

where the sequences b_j and c_j (1 \leq j \leq k) consist of given integer numbers. Your task is to compute a_n for a given and output the result modulo 10^9.

Giriş

The first line contains the integer (1 \leq t \leq 10^4), the number of test cases.

Each test case consists of four lines:

  • The first line contains a single integer (1 \leq k \leq 10) — the length of the sequences and .

  • The second line contains integers b_1, b_2, \ldots, b_k (0 \leq b_j \leq 10^9), the initial values of the sequence.

  • The third line contains integers c_1, c_2, \ldots, c_k (0 \leq c_j \leq 10^9), the coefficients of the recurrence.

  • The fourth line contains a single integer (1 \leq n \leq 10^9) — the required position in the sequence.

Çıktı

For each test case, print a single integer: the value of a_n modulo 10^9.

Örnekler

Örnek

Giriş

2
3
1 2 3
2 1 3
4
3
1 2 3
2 1 3
6
Çıktı
11
45

Örnek 1

Loading
Loading