#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <complex>
#include <string>
#include <sstream>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

#define all(c) c.begin(),c.end()
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

typedef long long ll;
typedef vector<char> v1;
typedef vector<v1> v2;
typedef vector<v2> v3;

template<typename T>
ostream& operator<<(ostream& os,const vector<T>& val){
    os << "[ ";
    for(typename vector<T>::const_iterator it=val.begin();
        it != val.end();++it){
        os << *it << " ";
    }
    os << "]";
    return os;
}
template<typename T>
ostream& operator<<(ostream& os,const set<T>& val){
    os << "[ ";
    for(typename set<T>::const_iterator it=val.begin();
        it != val.end();++it){
        os << *it << " ";
    }
    os << "]";
    return os;
}

typedef set<char> si;
const int size = 5;

v3 solve(const int& N,const v3& field,const si& born,const si& death){
    if(N == 0) return field;
    else{
        v3 new_field = field;
        rep(z,size) rep(y,size) rep(x,size){
            int cnt = 0;
            for(int dz=-1;dz<=1;dz++){
                for(int dy=-1;dy<=1;dy++){
                    for(int dx=-1;dx<=1;dx++){
                        if(dz == 0 and dy == 0 and dx == 0) continue;
                        int nz = z + dz;
                        int ny = y + dy;
                        int nx = x + dx;
                        if(0 <= nz and nz <= 4 and
                           0 <= ny and ny <= 4 and
                           0 <= nx and nx <= 4){
                            if(field[nz][ny][nx] == '1')
                                cnt += 1;
                        }
                    }
                }
            }

            // cerr << cnt << " " << (death.find(cnt) != death.end()) << " " <<
            //     (born.find(cnt) != born.end()) << endl;
            if(born.find(cnt) != born.end() and
               new_field[z][y][x] == '0'){
                new_field[z][y][x] = '1';
            }else if(death.find(cnt) == death.end() and
                     new_field[z][y][x] == '1'){
                new_field[z][y][x] = '0';
            }
        }
        return solve(N-1,new_field,born,death);
    }
}
int main(){
    for(int test_case=1;;test_case++){
        int N;
        cin >> N;
        if(N == 0) break;
        if(test_case != 1)
            cout << endl;

        v3 field(size,v2(size,v1(size)));
        rep(z,size) rep(y,size) rep(x,size){
            cin >> field[z][y][x];
        }
        set<char> born;
        set<char> death;
        int m1;
        cin >> m1;
        for(int i=0;i<m1;i++){
            int x;
            cin >> x;
            born.insert(x);
        }
        int m2;
        cin >> m2;
        for(int i=0;i<m2;i++){
            int x;
            cin >> x;
            death.insert(x);
        }
        cout << "Case " << test_case << ":" << endl;
        v3 ret = solve(N,field,born,death);
        for(int z=0;z<size;z++){
            for(int y=0;y<size;y++){
                for(int x=0;x<size;x++){
                    cout << ret[z][y][x];
                }
                cout << endl;
            }
            if(z != size-1)
                cout << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 1400