extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet};
use std::fmt::{Display, Formatter, Error};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

struct Coordinate {
    x: i32, y: i32, z: i32
}
impl Coordinate {
    fn neighbors(&self) -> Vec<Coordinate> {
        let mut result = Vec::with_capacity(26);
        for i in -1 .. 2 {
            for j in -1 .. 2 {
                for k in -1 .. 2 {
                    if i != 0 || j != 0 || k != 0 {
                        result.push(Coordinate{x: self.x + i, y: self.y + j, z: self.z + k});
                    }
                }
            }
        }
        result
    }
}
fn main() {
    let mut case = 1;
    loop {
        let_all!(n: usize);
        if n == 0 {
            return
        }
        if case != 1 {
            println!();
        }
        let mut state = Vec::with_capacity(5);
        for _ in 0 .. 5 {
            let mut square: Vec<Vec<bool>> = Vec::with_capacity(5);
            for _ in 0 .. 5 {
                square.push(read_line!().trim().chars().map(|x| x == '1').collect::<Vec<bool>>());
            }
            state.push(square);
            read_line!();
        }
        let a: Vec<i32> = read_line!(' '; i32);
        let b: Vec<i32> = read_line!(' '; i32);
        let mut state = vec![state, vec![vec![vec![false; 5]; 5]; 5]];
        for day in 0 .. n {
            for x in 0 .. 5 {
                for y in 0 .. 5 {
                    for z in 0 .. 5 {
                        if !state[day % 2][x][y][z] {
                            let mut count = 0;
                            for neighbor in (Coordinate{x: x as i32, y: y as i32, z: z as i32}).neighbors() {
                                if 0 <= neighbor.x && neighbor.x < 5 &&
                                    0 <= neighbor.y && neighbor.y < 5 &&
                                    0 <= neighbor.z && neighbor.z < 5 &&
                                    state[day % 2][neighbor.x as usize][neighbor.y as usize][neighbor.z as usize]{
                                    count += 1;
                                }
                            }
                            state[(day + 1) % 2][x][y][z] = false;
                            for i in 1 .. a.len() {
                                if count == a[i] {
                                    state[(day + 1) % 2][x][y][z] = true;
                                    break
                                }
                            }
                        }else {
                            let mut count = 0;
                            for neighbor in (Coordinate{x: x as i32, y: y as i32, z: z as i32}).neighbors() {
                                if 0 <= neighbor.x && neighbor.x < 5 &&
                                    0 <= neighbor.y && neighbor.y < 5 &&
                                    0 <= neighbor.z && neighbor.z < 5 &&
                                    state[day % 2][neighbor.x as usize][neighbor.y as usize][neighbor.z as usize]{
                                    count += 1;
                                }
                            }
                            state[(day + 1) % 2][x][y][z] = false;
                            for i in 1 .. b.len() {
                                if count == b[i] {
                                    state[(day + 1) % 2][x][y][z] = true;
                                    break
                                }
                            }
                        }
                    }
                }
            }
        }
        let result = &state[n as usize % 2];
        println!("Case {}:", case);
        case += 1;
        for i in 0 .. 5 {
            if i != 0 {
                println!();
            }
            for line in &result[i] {
                println!("{}", line.iter().map(|&x| if x {"1".to_string()} else {"0".to_string()}).collect::<Vec<_>>().connect(""));
            }
        }

    }
}


Measured memory_bytes: 5056