#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 15
#define mod 1000000000
ll k;
ll b[maxn], c[maxn], n;
struct matran
{
ll a[maxn][maxn];
void print()
{
for (ll i = 0; i < k; i++)
{
for (ll j = 0; j < k; j++)
cout << a[i][j] << " ";
cout << '\n';
}
}
};
matran mot, M;
struct matran_1
{
ll a[1][maxn];
void print()
{
for (ll i = 0; i < 1; i++)
{
for (ll j = 0; j < k; j++)
cout << a[i][j] << " ";
cout << '\n';
}
}
};
matran prod(matran A, matran B)
{
matran C;
for (ll i = 0; i < k; i++)
for (ll j = 0; j < k; j++)
C.a[i][j] = 0;
for (ll i = 0; i < k; i++)
for (ll j = 0; j < k; j++)
for (ll kk = 0; kk < k; kk++)
C.a[i][j] = (C.a[i][j] + (A.a[i][kk] * B.a[kk][j] % mod)) % mod;
return C;
}
matran_1 prod1(matran_1 A, matran B)
{
matran_1 C;
for (ll i = 0; i < 1; i++)
for (ll j = 0; j < k; j++)
C.a[i][j] = 0;
for (ll i = 0; i < 1; i++)
for (ll j = 0; j < k; j++)
for (ll kk = 0; kk < k; kk++)
C.a[i][j] = (C.a[i][j] + (A.a[i][kk] * B.a[kk][j] % mod)) % mod;
return C;
}
matran po(matran A, ll n)
{
matran res = A, ans = mot;
while (n)
{
if (n % 2)
ans = prod(ans, res);
res = prod(res, res);
n /= 2;
}
return ans;
}
void solve()
{
cin >> k;
for (ll i = 0; i < k; i++)
cin >> b[i];
for (ll i = 0; i < k; i++)
cin >> c[i];
cin >> n;
matran_1 inita;
for (ll j = 0; j < k; j++)
inita.a[0][j] = b[k-1-j];
matran_1 res;
if (n < k)
{
cout << b[n - 1] << '\n';
return;
}
ll unit[k][k];
for (ll i = 0; i < k; i++)
for (ll j = 0; j < k; j++)
if (i == j)
unit[i][j] = 1;
else
unit[i][j] = 0;
ll tmp[k][k];
for (ll i = 0; i < k; i++)
{
for (ll j = 0; j < k; j++)
if (j == 0)
tmp[i][j] = c[i];
else if (j - i == 1)
tmp[i][j] = 1;
else
tmp[i][j] = 0;
}
for(ll i=0;i<k;i++)
for(ll j=0;j<k;j++){
mot.a[i][j] = unit[i][j];
M.a[i][j] = tmp[i][j];
}
res = prod1(inita, po(M, n - k));
cout << res.a[0][0] << '\n';
}
int main()
{
ll testcase;
cin >> testcase;
while (testcase--)
solve();
return 0;
}