#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
const int N = 1e6+10;
int t,n,m,k,l,r,op,x,y,ans;
int f[N];
void solve(){
cin>>t;
while(t--){
ans = 0;
cin>>n;
for(int i = 0;i<=n;i++){
cin>>f[i];
}
cin>>x;
for(int i = 0;i<=n;i++){
ans += f[i] * pow(x,i);
}
cout<<ans<<"\n";
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}