#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
const LL inf=~0ULL>>1;
struct Node {
	LL damage,cost;	
}P[300000];
LL H[10],D[10],C[10];	int B[10];
bool cmp(const Node &x,const Node &y) {
	if(x.damage != y.damage)	return x.damage < y.damage;
	return x.cost < y.cost;	
}
bool cmp1(int x,int y) {
	return H[x]<H[y];
}
void shuffle() {
	static LL h[10],d[10],c[10];
	rep(i,5)	h[i]=H[i],d[i]=D[i],c[i]=C[i];
	rep(i,5)	H[i]=h[B[i]],D[i]=d[B[i]],C[i]=c[B[i]];
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("D.in", "r", stdin);
    freopen("D.out", "w", stdout);
#endif
    // ios::sync_with_stdio(false);		cin.tie(0);
    int T;	scanf("%d", &T);
    while(T--) {
    	LL L;		scanf("%lld",&L);    	
    	rep(i,5)	scanf("%lld%lld%lld",H+i,D+i,C+i);
    	rep(i,5)	B[i]=i;
    	sort(B,B+5,cmp1);
    	shuffle();    	
    	int k=0;    	
    	FOR(i,0,H[3])
    	FOR(j,0,H[4]) {
    		LL d=D[3]*i+D[4]*j, cost=C[3]*i+C[4]*j, sum=i+j;
    		P[k++]=(Node){d,cost};    		
    	}    	
    	sort(P,P+k,cmp);    	    	
    	static Node tmp[300000];
    	int m=0;    	
    	rep(i,k) {
    		while(m && tmp[m-1].cost>=P[i].cost)	--m;
    		if(m && tmp[m-1].damage==P[i].damage && tmp[m-1].cost<=P[i].cost)	continue;
    		tmp[m++]=P[i];
    	}    	
    	rep(i,m)	P[i]=tmp[i];    	    	
    	pair<LL,LL> res=MP(inf,inf);
    	int h[3];
    	for(h[0]=0;h[0]<=H[0];++h[0])
    	for(h[1]=0;h[1]<=H[1];++h[1])
    	for(h[2]=0;h[2]<=H[2];++h[2]) {    		
			LL d1=0,c1=0;
			rep(i,3)	d1+=h[i]*D[i],c1+=h[i]*C[i];
			if(d1>=L) {				
				res=min(res, MP(c1,d1));
				continue;
			}			
			LL d2=L-d1;			
			int l=0,r=m;
			while(l<r) {
				int mid=(l+r)>>1;
				if(P[mid].damage>=d2)	r=mid;
				else					l=mid+1;
			}
			if(l==m)	continue;
			LL c2=P[l].cost; d2=P[l].damage;			
			res=min(res, MP(c1+c2,(d1+d2)));						
    	}
    	if(res.first==inf)	puts("We are doomed!!");    							  
    	else				printf("%lld %lld\n",res.first,res.second);
    }
    return 0;
}