SEQ  Recursive Sequence
Sequence (a_{i}) of natural numbers is defined as follows:
a_{i} = b_{i} (for i <= k)
a_{i} = c_{1}a_{i1} + c_{2}a_{i2} + ... + c_{k}a_{ik} (for i > k)
where b_{j} and c_{j} are given natural numbers for 1<=j<=k. Your task is to compute a_{n} for given n and output it modulo 10^{9}.
Input
On the first row there is the number C of test cases (equal to about 1000).
Each test contains four lines:
k  number of elements of (c) and (b) (1 <= k <= 10)
b_{1},...,b_{k}  k natural numbers where 0 <= b_{j} <= 10^{9} separated by spaces
c_{1},...,c_{k}  k natural numbers where 0 <= c_{j} <= 10^{9} separated by spaces
n  natural number (1 <= n <= 10^{9})
Output
Exactly C lines, one for each test case: a_{n} modulo 10^{9}
Example
Input: 3 3 5 8 2 32 54 6 2 3 1 2 3 4 5 6 6 3 24 354 6 56 57 465 98765432 Output: 8 714 257599514
hide comments
hellb0y_suru:
20200219 15:38:33
Matrix exponentiation !!!


lm10_piyush:
20200215 16:37:11
damn hogya!!! if is allowed to provide the solution like here I can post it...


harish_49:
20191103 12:37:34
AC in 0 go !!! Nice problem 

devinamuljono:
20191019 09:58:23
manage to get accepted after dealing with some edge case , 

saifu:
20191013 09:38:06
I am getting segmentation fault for 3rd test case. Please help me. 

davemaharshi7:
20191004 16:48:37
Anybody here after Coding Blocks tutorial on this ... 

neerajamoniya:
20190926 08:20:01
Why is recursion giving TLE ???? 

divyanshu12312:
20190806 14:05:59
Long Implementation


smiling_addu:
20190728 05:59:57
WA in one go.... :) 

maratha:
20190726 19:14:21
You might find problem 'Retrospective Sequences' on codeforces gym similar to this.(Slight variation) Last edit: 20190726 19:15:33 
Added by:  PaweÅ‚ Dobrzycki 
Date:  20050429 
Time limit:  0.5s3s 
Source limit:  8196B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All 
Resource:  IV Podlasian Contest in Team Programming 