#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct Player{
    int h, d, c;
    void get() {
        scanf("%d%d%d",&h,&d,&c);
    }
    bool operator<(const Player &r) const {
        return h<r.h;
    }
} p[5];
typedef pair<LL, LL> pii;
typedef vector<pii> vpii;
#define mp make_pair
#define pb push_back
#define REP(i,n) for(int i=0; i<int(n); ++i)
vpii s0, s1;
void uniq(vpii &s){
    int r=0;
    REP(i,s.size()){
        while(r>0 && s[i].second<s[r-1].second)--r;
        s[r++]=s[i];
    }
    s.resize(r);
	REP(i,s.size()-1){
		assert(s[i+1].first>=s[i].first);
		assert(s[i+1].second>=s[i].second);
	}
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
		while(s0.size())s0.pop_back();
		while(s1.size())s1.pop_back();
        LL L;
        scanf("%lld", &L);
        REP(i,5)p[i].get();
        sort(p, p+5);
        for(int a=0; a<=p[0].h; ++a){
            LL c0 = p[0].c * (LL)a, d0 = p[0].d * (LL)a;
            for(int b=0; b<=p[1].h; ++b){
                LL c1 = c0 + p[1].c * (LL)b, d1 = d0 + p[1].d * (LL)b;
                for(int c=0; c<=p[2].h; ++c){
                    LL c2 = c1 + p[2].c * (LL)c, d2 = d1 + p[2].d * (LL)c;
                    s0.pb(mp(d2, c2));
                }
            }
        }
        stable_sort(s0.begin(), s0.end());
        uniq(s0);
        reverse(p, p+5);
        for(int a=0; a<=p[0].h; ++a){
            LL c0 = p[0].c * (LL)a, d0 = p[0].d * (LL)a;
            for(int b=0; b<=p[1].h; ++b){
                LL c1 = c0 + p[1].c * (LL)b, d1 = d0 + p[1].d * (LL)b;
                s1.pb(mp(d1, c1));
            }
        }
        stable_sort(s1.begin(), s1.end());
        uniq(s1);
        reverse(s1.begin(), s1.end());
		pii ans=mp(-1,-1);
        for(int i=0, j=0; i<s0.size(); ++i){
            while(j+1<s1.size() && s1[j+1].first+s0[i].first>=L)++j;
            pii nans=mp(s1[j].second+s0[i].second, s1[j].first+s0[i].first);
            if(nans.second>=L){
                if(ans.first==-1)ans=nans;
                else if(nans < ans)ans=nans;
            }
        }
        if(ans.first == -1)puts("We are doomed!!");
        else printf("%lld %lld\n", ans.first, ans.second);
    }
}
