#include<iostream>
using namespace std;
void bubblesort(int *arr,int n){
for(int i=0;i<n;++i)
for(int j=1;j<n-i;++j)
if(arr[j-1]>arr[j]){
int tmp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = tmp;
}
}
int ffib(int a, int b, int c, int d, int x){
int f[x];
f[0]=a;
f[1]=b;
f[2]=c;
f[3]=d;
for (int i=4; i<x; i++) {
f[i]=f[i-4]+f[i-1];
}
bubblesort(f, x);
return f[(x-1)/2];
}
int main(){
int a,b,c,d,x;
int n;
while (cin>>n) {
int ans[100];
int count=0;
for (int i=0; i<n; i++) {
cin>>x>>a>>b>>c>>d;
ans[count]=ffib(a, b, c, d, x);
count++;
}
for (int i=0; i<count; i++) {
cout<<ans[i]<<endl;
}
}
return 0;
}