//NAME:JARDAN Andrei
//GRADE:11
//DIVISION:sr3
//SCHOOL:NCSC "Gr. Moisil"

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
typedef priority_queue, std::greater> backwards_queue;

string dr = "VVHHHHVHHHHVHHHHVHHHHVVVVHVVVVHHHHVHHHHVHHHHVHHHHVV";

ifstream fin("patolli.in");
ostream &out = cout;

bool inbounds(int a){
	return a >= 1 && a <= 52;
}

bool isprime(int na){
	if(na <= 1){
		return false;
	}
	for(int i = 2; i*i <= na; ++i){
		if(na%i == 0){
			return false;
		}
	}
	return true;
}

bool isperfect(int na){
	for(int i = 3; i*i <= na; ++i){
		if(i*i == na){
			return true;
		}
	}
	return false;
}

bool ishv(int na, int a){
	for(int i = a; i < na-1; ++i){
		if(dr[i-1] == 'H' && dr[i] == 'V'){
			return true;
		}
	}
	return false;
}

int cpl;
backwards_queue pls[2];
bool viz[114];
int n;
void nuke(){
	for(int i = 0; i <= 52; ++i){
		viz[i] = false;
	}
	pls[0] = backwards_queue();
	pls[1] = backwards_queue();
	cpl = 0;
}

void read(){
	for(int i = 0; i < 2; ++i){
		for(int j = 0; j < 3; ++j){
			int a;fin >> a;
			if(a != 52){
				pls[i].push(a);
				viz[a] = true;
			}

		}
	}
	fin >> n;
}

int sign(int a){
	if(a > 0){
		return 1;
	}else if(a == 0){
		return 0;
	}else{
		return -1;
	}
}

int slowcrawl(int na, int v){
	int inc = sign(v);
	while(v != 0 && inbounds(na+inc) && !viz[na+inc]){
		na += inc;
		v -= inc;
	}
	return na;
}

void solve(){
	int r;fin >> r;
	
	backwards_queue &pl = pls[cpl];
	
	if(!pl.empty()){
		int a = pl.top();
		int na = a+r;
		if(inbounds(na) && !viz[na]){
			pl.pop();
			viz[a] = false;
			if(na != 52){
				if(isprime(na)){
					na = slowcrawl(na, 6);
				}else if(isperfect(na)){
					na = slowcrawl(na, -6);
				}else if(ishv(na, a)){
					while(na > a && (na%r != 0 || viz[na])){
						na--;
					}
				}
				if(na != 52){
					viz[na] = true;
					pl.push(na);
				}
			}
		}
	}
	
	cpl = 1-cpl;
}

void write(){
	int s1 = 0, s2 = 0;
	while(!pls[0].empty()){
		s1 += pls[0].top();
		pls[0].pop();
	}
	while(!pls[1].empty()){
		s2 += pls[1].top();
		pls[1].pop();
	}
	out << s1 << " " << s2 << "\n";
}

int main(){
	for(int acsl = 0; acsl < 5; ++acsl){
		nuke();
		read();
		for(int i = 0; i < n; ++i){
			solve();
		}
		write();
	}
	return 0;
}