Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <array>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int CO = 0;
bool F = 0;
int n;
int field[10][10][10];//zyx
int m1, m2;
int a[100], b[100];
string s;

int search(int z, int y, int x){
	int co = 0;

	for (int i = -1; i <= 1; i++){
		for (int j = -1; j <= 1; j++){
			for (int k = -1; k <= 1; k++){
				if (i == 0 && j == 0 && k == 0)continue;
				int nz = z + k, ny = y + j, nx = x + i;
				if (nz >= 0 && nz < 5 && ny >= 0 && ny < 5 && nx >= 0 && nx < 5){
					if (field[nz][ny][nx])co++;
				}
			}
		}
	}

	//cout << field[z][y][x] << "!" << co << endl;
	/*REP(i, m1){
		if (co == a[i])return 1;
	}
	REP(i, m2){
		if (co == b[i])return 0;
		if (i == m2 - 1)return -1;
	}

	return 0;*/
	return co;
}

bool check(int m, int ar[], int co){
	REP(i, m){
		if (ar[i] == co)return true;
	}
	return false;
}

int main(){
	while (cin >> n&&n){
		CO++;
		REP(i, 5){
			REP(j, 5){
				cin >> s;
				REP(k, 5){
					field[i][j][k] = s[k] - '0';
				}
			}
		}

		cin >> m1;
		REP(i, m1)cin >> a[i];
		cin >> m2;
		REP(i, m2)cin >> b[i];

		REP(l, n){
			//cout << l << endl;
			int newfield[10][10][10];
			REP(i, 5){
				REP(j, 5){
					REP(k, 5){
						int num = search(i, j, k);
						if (field[i][j][k] == 0){
							if (check(m1, a, num))newfield[i][j][k] = 1;
							else newfield[i][j][k] = 0;
						}
						else{
							if (!check(m2, b, num))newfield[i][j][k] = 0;
							else newfield[i][j][k] = 1;
						}
					}
				}
			}
			REP(i, 5){
				REP(j, 5){
					REP(k, 5){
						field[i][j][k] = newfield[i][j][k];
						//cout << field[i][j][k];
					}
					//cout << endl;
				}
				//cout << endl;
			}
		}

		if (F)cout << endl;
		cout << "Case " << CO << ":" << endl;
		REP(i, 5){
			REP(j, 5){
				REP(k, 5){
					cout << field[i][j][k];
				}
				cout << endl;
			}
			if (i != 4)cout << endl;
		}
		F = 1;
	}
}

Compiler output:
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of '_ForwardIterator std::search(_ForwardIterator, _ForwardIterator, const _Searcher&) [with _ForwardIterator = long long int; _Searcher = long long int]':
a.cc:105:23:   required from here
  105 |                                                 int num = search(i, j, k);
      |                                                           ~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4186:24: error: expression cannot be used as a function
 4186 |     { return __searcher(__first, __last).first; }
      |              ~~~~~~~~~~^~~~~~~~~~~~~~~~~