#include <vector>
#include <iostream>
#define rep(i,n) for (int i = 1; i <= n; i++)
using namespace std;
typedef long long int ll;
typedef vector<vector<ll> > matrix;
const ll mod = 1000000000;
matrix mul(matrix A,matrix B,int k){
matrix c(k+1,vector<ll>(k+1));
for(int i=1;i<=k;i++){
for(int j=1;j<=k;j++){
for(int l=1;l<=k;l++){
c[i][j]=(c[i][j]+(A[i][l]*B[l][j]))%mod;
}
}
}
return c;
}
matrix pow(matrix A,int p,int k){
if(p==1)
return A;
if(p%2){
return mul(A,pow(A,p-1,k),k);
}
matrix X=pow(A, p/2,k);
return mul(X,X,k);
}
int fib(int N,vector<ll>b1,vector<ll>c1,int k){
vector<ll>bb(k+1);
rep(i,k){
bb[i]=b1[i];
}
matrix T(k+1,vector<ll>(k+1));
for(int y=1;y<=k;y++){
for(int u=1;u<=k;u++){
if(y+1==u)
T[y][u]=1;
}
}
rep(i,k){
int y=c1.back();
c1.pop_back();
T[k][i]=y;
}
if(N<k){
cout<<b1[N]<<endl;
return 0;
}
T=pow(T,N-1,k);
ll res=0;
for(int i=1;i<=k;i++)
res=(res+T[1][i]*bb[i])%mod;
cout<<res<<endl;
}
int main(){
int cas,k,b,c,n;
cin>>cas;
rep(i,cas)
{
cin>>k;
vector<ll>b1(k+1);
vector<ll>c2(k+1);
rep(j,k){
cin>>b;
b1[j]=b;
}
rep(o,k){
cin>>c;
c2[o]=c;
}
cin>>n;
fib(n,b1,c2,k);
}
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKI2RlZmluZSByZXAoaSxuKSBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBpbnQgbGw7CnR5cGVkZWYgdmVjdG9yPHZlY3RvcjxsbD4gPiBtYXRyaXg7CmNvbnN0IGxsIG1vZCA9IDEwMDAwMDAwMDA7CgptYXRyaXggbXVsKG1hdHJpeCB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ogICB7ICAgCiAgICAgIAogICAgIGNpbj4+azsKICAgICAgIHZlY3RvcjxsbD5iMShrKzEpOwogICB2ZWN0b3I8bGw+YzIoaysxKTsKICAKICAKICAgIHJlcChqLGspewogICAgICAgICAKICAgICAgY2luPj5iOwogICAgICBiMVtqXT1iOwogICAgIAogICAgfQogICAgIHJlcChvLGspewogICAgIAogICAgICAgCiAgICAgICAgY2luPj5jOwogICAgICAgIGMyW29dPWM7CiAgICAgIH0KICAgICAgY2luPj5uOwoKICAgICAgZmliKG4sYjEsYzIsayk7CiAgICAgIAogICB9CgoKfQ==