Problem: It was the last day of the summer camp you strayed into the labyrinth on the way to Komaba Campus, the University of Tokyo. The contest has just begun. Your teammates must impatiently wait for you. So you have to escape from this labyrinth as soon as possible.
The labyrinth is represented by a grid map. Initially, each grid except for walls and stairs is either on the first floor or on the second floor. Some grids have a switch which can move up or down some of the grids (the grids on the first floor move to the second floor, and the grids on the second floor to the first floor).
In each step, you can take one of the following actions:


* Move to an adjacent grid (includes stairs) on the same floor you are now in.

* Move to another floor (if you are in the stairs grid).

* Operate the switch (if you are in a grid with a switch).




Luckily, you have just found a map of the labyrinth for some unknown reason. Let's calculate the minimum step to escape from the labyrinth, and go to the place your teammates are waiting!



Input

The format of the input is as follows.


> W H
> M11M12M13...M1W
> M21M22M23...M2W
> ........
> MH1MH2MH3...MHW
> S
> MS111MS112MS113...MS11W
> MS121MS122MS123...MS12W
> ........
> MS1H1MS1H2MS1H3...MS1HW
> MS211MS212MS213...MS21W
> MS221MS222MS223...MS22W
> ........
> MS2H1MS2H2MS2H3...MS2HW
> MSS11MSS12MSS13...MSS1W
> MSS21MSS22MSS23...MSS2W
> ........
> MSSH1MSSH2MSSH3...MSSHW
>

The first line contains two integers W (3 ≤ W ≤ 50) and H (3 ≤ H ≤ 50). They represent the width and height of the labyrinth, respectively.
The following H lines represent the initial state of the labyrinth. Each of Mij is one of the following symbols:


* '#' representing a wall,
* '|' representing stairs,
* '_' representing a grid which is initially on the first floor,
* '^' representing a grid which is initially on the second floor,
* a lowercase letter from 'a' to 'j' representing a switch the grid has, and the grid is initially on the first floor,
* an uppercase letter from 'A' to 'J' representing a switch the grid has, and the grid is initially on the second floor,
* '%' representing the grid you are initially in (which is initially on the first floor) or
* '&' representing the exit of the labyrinth (which is initially on the first floor).



The next line contains one integer S (0 ≤ S ≤ 10), and then the following SH lines represent the information of the switches. Each of MSkij is one of:


* '#' if Mij is a '#',
* '|' if Mij is a '|',
* '*' if the grid is moved by the switch represented by the k-th alphabet letter, or
* '.' otherwise.



Note that the grid which contains a switch may be moved by operating the switch. In this case, you will move together with the grid.
You may assume each of the '%' (start) and '&' (goal) appears exacyly once, that the map is surround by walls, and that each alphabet in the map is any of the letters from 'A' (or 'a') to S-th alphabet letter.

Output

Print the minimum step to reach the goal in one line. If there is no solution, print "-1".

Examples

Input

6 6
######
#_|A%#
#B#_|#
#^BBa#
#B&A##
######
2
######
#*|*.#
#.#.|#
#*.**#
#...##
######
######
#*|*.#
#*#.|#
#..**#
#..*##
######


Output

21


Input

6 6

_|A%#
B#_|#
^BBa#
B&A##

2

*|*.#
.#.|#
*.**#
...##


*|*.#
*#.|#
..**#
..*##


Output

21


Input

8 3

%||Aa&#

2

*||*..#


.||*.*#


Output

7


Input

3 4

%#
&#

0


Output

1


Input

3 5

%#
^#
&#

0


Output

-1

Solution (in CPP):

Solution:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 55, ST = 1 << 10, dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}, INF = 1000000000, Q = ST * N * N / 10;
int n, m, sx, sy, ex, ey, k, qhead, qtail, q[Q][4], dis[N][N][2][ST];
char map[N][N], col[10][N][N], pre[ST][N][N];

void change (char &grid) {
	if (grid == '^') {
		grid = '_';
	} else if (grid == '_') {
		grid = '^';
	}
	if (grid >= 'a' && grid <= 'j') {
		grid = 'A' + grid - 'a';
	} else if (grid >= 'A' && grid <= 'J') {
		grid = 'a' + grid - 'A';
	}
}

int height (const char &a) {
	return a == '_' || a >= 'a' && a <= 'j' ? 0 : 1;
}

int shift (const char &a) {
	if (a >= 'a' && a <= 'j') return a - 'a';
	if (a >= 'A' && a <= 'J') return a - 'A';
	return -1;
}

int main() {
	scanf("%d %d", &m, &n);
	for (int i = 0; i < n; ++i) {
		scanf("%s", map[i]);
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (map[i][j] == '%') {
				sx = i;
				sy = j;
			}
			if (map[i][j] == '&') {
				ex = i;
				ey = j;
			}
		}
	}
	scanf("%d", &k);
	for (int i = 0; i < k; ++i) {
		for (int j = 0; j < n; ++j) {
			scanf("%s", col[i][j]);
		}
	}
	for (int i = 0; i < 1 << k; ++i) {
		for (int r = 0; r < n; ++r) {
			for (int c = 0; c < m; ++c) {
				pre[i][r][c] = map[r][c];
				if (pre[i][r][c] == '%' || pre[i][r][c] == '&') pre[i][r][c] = '_';
			}
		}
		for (int j = 0; j < k; ++j) {
			if (i >> j & 1) {
				for (int r = 0; r < n; ++r) {
					for (int c = 0; c < m; ++c) {
						if (col[j][r][c] == '*') {
							change(pre[i][r][c]);
						}
					}
				}
			}
		}
	}
	for (int x = 0; x < n; ++x) {
		for (int y = 0; y < m; ++y) {
			for (int z = 0; z < 2; ++z) {
				for (int st = 0; st < 1 << k; ++st) {
					dis[x][y][z][st] = INF;
				}
			}
		}
	}
	dis[sx][sy][0][0] = 0;
	qhead = 0; qtail = 1; q[1][0] = sx; q[1][1] = sy; q[1][2] = 0; q[1][3] = 0;
	while (qhead != qtail) {
		++qhead;
		if (qhead == Q) qhead = 0;
		int nx = q[qhead][0], ny = q[qhead][1], nz = q[qhead][2], nst = q[qhead][3];
//cout << nx << ' ' << ny << ' ' << nz << ' ' << pre[nst][nx][ny] << ' ' << height(pre[nst][nx][ny]) << ' ' << shift(pre[nst][nx][ny]) << endl;
		for (int d = 0; d < 4; ++d) {
			int tx = nx + dx[d], ty = ny + dy[d];
			if (tx >= 0 && tx < n && ty >= 0 && ty < m && pre[nst][tx][ty] != '#' && (pre[nst][tx][ty] == '|' || height(pre[nst][tx][ty]) == nz) && dis[tx][ty][nz][nst] == INF) {
				dis[tx][ty][nz][nst] = dis[nx][ny][nz][nst] + 1;
				++qtail; if (qtail == Q) qtail = 0;
				q[qtail][0] = tx; q[qtail][1] = ty; q[qtail][2] = nz; q[qtail][3] = nst;
			}
		}
		if (map[nx][ny] == '|' && dis[nx][ny][nz ^ 1][nst] == INF) {
			dis[nx][ny][nz ^ 1][nst] = dis[nx][ny][nz][nst] + 1;
			++qtail; if (qtail == Q) qtail = 0;
			q[qtail][0] = nx; q[qtail][1] = ny; q[qtail][2] = nz ^ 1; q[qtail][3] = nst;
		}
		if (shift(pre[nst][nx][ny]) != -1) {
			int tst = nst ^ (1 << shift(pre[nst][nx][ny])), tz = nz;
			if (pre[tst][nx][ny] != '|') tz = height(pre[tst][nx][ny]);
			if (dis[nx][ny][tz][tst] == INF) {
				dis[nx][ny][tz][tst] = dis[nx][ny][nz][nst] + 1;
				++qtail; if (qtail == Q) qtail = 0;
				q[qtail][0] = nx; q[qtail][1] = ny; q[qtail][2] = tz; q[qtail][3] = tst;
			}
		}
	}
	int ans = INF;
	for (int z = 0; z < 2; ++z) {
		for (int st = 0; st < 1 << k; ++st) {
			ans = min(ans, dis[ex][ey][z][st]);
		}
	}
	printf("%d\n", ans == INF ? -1 : ans);
	return 0;
}