/*******************************************
    School: Longfellow Middle School
    Name: Shinyoung Jeon (Catherine)
    Grade: 8th
    Division: INT-3
    Program Language: C++
*******************************************/
#include 
#include 
using namespace std;

int pl[3], opp[3], n;
int hzmoves[9] = {6, 11, 16, 21, 26, 34, 39, 44, 49};
int ifPrime(int num){
   for(int k = 1; k <= 6; k ++) {
      for(int j = 0; j < 3; j ++) {
         if(num + k == opp[j] || num + k == pl[j]) {
            return num + k - 1;
         }
      }
   }
   return num + 6;
}
bool prime(int num) {
   int m = num/2;
   for(int i = 2; i <= m; i ++){
      if(num % i == 0){
         return false;
      }
   }
   return true;
}
int ifSquare(int num){
   for(int k = 1; k <= 6; k ++) {
      for(int j = 0; j < 3; j ++) {
         if(num - k == opp[j] || num - k == pl[j]) {
            return num - k + 1;
         }
      }
   }
   return num - 6;
}
bool square(int num) {
   int arr[5] = {9, 16, 25, 36, 49};
   for(int i = 0; i < 5; i ++) {
      if(num == arr[i]) {
         return true;
      }
   }
   return false;
}
bool hzmove(int num, int diceroll) {
   for(int i = 0; i < 9; i ++) {
      if(num - diceroll <= hzmoves[i] &&  hzmoves[i] + 2 <= num) 
         return true;
   }
   return false;
}
int hznextmove(int num, int diceroll) {
   int correct = 0;
   for(int i = 1; i < 52; i ++) {
      for(int j = 0; j < 3; j ++) {
         if(num < diceroll * i && num + diceroll >= diceroll * i && diceroll * i != opp[j] && diceroll * i != pl[j]) correct ++;
      }
      if(correct == 3) {
         return diceroll * i;
      }
      correct = 0;
   }
   return num;
}
bool order(int a, int b, int c){
   if(a < b && b < c) 
      return false;
   return true;
}

int main() {
   ifstream inputFile;
   string filename;
   cout << "Enter the file name: ";
   cin >> filename;
   inputFile.open(filename);
   for(int i = 0; i < 5; i ++) {
      for(int i = 0; i < 3; i ++) {
         inputFile >> opp[i];
      }
      for(int i = 0; i < 3; i ++) {
         inputFile >> pl[i];
      }
      if(order(pl[0], pl[1], pl[2])) {
         int min = 0;
         for(int z = 0; z < 2; z ++) {
            min = pl[z];
            for(int i = z + 1; i < 3; i ++) {
               if(min <= pl[i]) {}
               else {
                  min = pl[i];
                  pl[i] = pl[z];
                  pl[z] = min;
               }
            }
         }
      }
      inputFile >> n;
      int dr[n];
      int num = 0;
      for(int i = 0; i < n; i ++) {
         inputFile >> dr[i];
         num = pl[0];
         if(num + dr[i] > 52) 
            continue;
         for(int j = 0; j < 3; j ++) {
            if(num + dr[i] == opp[j] || num + dr[i] == pl[j]) 
               continue;
         }
         num = num + dr[i];
         if(num == pl[1] || num == pl[2] || num == opp[0] || num == opp[1] || num == opp[2]) 
            continue;
         if(square(num)) {
            num = ifSquare(num);
         }
         else if(prime(num)) {
            num = ifPrime(num);
         }
         else {
            if(hzmove(num, dr[i])) {
               int x = hznextmove(num - dr[i], dr[i]);
               if(x != -1) num = x;
            }
         }
         pl[0] = num;
         if(pl[0] > pl[1]) {
            if(pl[0] > pl[2]) {
               pl[0] = pl[1];
               pl[1] = pl[2];
               pl[2] = num;
            }
            else{
               pl[0] = pl[1];
               pl[1] = num;
            }
         }
      }
      int c = 0;
      for(int i = 0; i < 4; i ++) {
         if(i == 3) {
            cout << endl;
            break;
         }
         if(pl[i] != 52) cout << pl[i] << " ";
         else c ++;
         
      }
      if(c == 3) cout << "GAME OVER" << endl;
   }
}