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 JAVA):

Solution:
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;

// A Two Floors Dungeon
// 2012/09/22
public class Main{
	Scanner sc=new Scanner(System.in);

	int INF=1<<28;

	int w, h;
	int s;
	char[][] map;
	char[][][] maps;

	static final char WALL='#', STAIRS='|', FIRST='_', SECOND='^', START='%',
			GOAL='&';

	void run(){
		w=sc.nextInt();
		h=sc.nextInt();
		map=new char[h][];
		for(int i=0; i<h; i++){
			map[i]=sc.next().toCharArray();
		}
		s=sc.nextInt();
		maps=new char[s][h][];
		for(int j=0; j<s; j++){
			for(int i=0; i<h; i++){
				maps[j][i]=sc.next().toCharArray();
			}
		}
		solve();
	}

	void solve(){
		int xs=-1, ys=-1;
		int xg=-1, yg=-1;
		for(int j=0; j<h; j++){
			for(int i=0; i<w; i++){
				if(map[j][i]==START){
					xs=i;
					ys=j;
				}else if(map[j][i]==GOAL){
					xg=i;
					yg=j;
				}
			}
		}

		int[] dx={0, 0, -1, 1};
		int[] dy={-1, 1, 0, 0};

		boolean[][][] ok=new boolean[2][h][w];
		for(int j=0; j<h; j++){
			for(int i=0; i<w; i++){
				char c=map[j][i];
				ok[0][j][i]=c==STAIRS||c==FIRST||Character.isLowerCase(c)
						||c==START||c==GOAL;
				ok[1][j][i]=c==STAIRS||c==SECOND||Character.isUpperCase(c);
			}
		}

		int[][][][] d=new int[h][w][2][1<<s];
		for(int j=0; j<h; j++){
			for(int i=0; i<w; i++){
				fill(d[j][i][0], INF);
				fill(d[j][i][1], INF);
			}
		}
		d[ys][xs][0][0]=0;

		LinkedList<P> que=new LinkedList<P>();
		que.offer(new P(xs, ys, 0, 0));

		for(; !que.isEmpty();){
			P p=que.poll();
			int virtualFloor=p.floor;
			for(int i=0; i<s; i++){
				if((p.sw>>i&1)==1&&maps[i][p.y][p.x]=='*'){
					virtualFloor=1-virtualFloor;
				}
			}
			LinkedList<P> next=new LinkedList<P>();
			if(map[p.y][p.x]==STAIRS){
				next.add(new P(p.x, p.y, 1-p.floor, p.sw));
			}
			if(Character.isLetter(map[p.y][p.x])){
				int sw=Character.toLowerCase(map[p.y][p.x])-'a';
				if(maps[sw][p.y][p.x]=='*'){
					next.add(new P(p.x, p.y, 1-p.floor, p.sw^(1<<sw)));
				}else{
					next.add(new P(p.x, p.y, p.floor, p.sw^(1<<sw)));
				}
			}
			for(int k=0; k<4; k++){
				P q=new P(p.x+dx[k], p.y+dy[k], p.floor, p.sw);
				int virtualFloor2=q.floor;
				for(int i=0; i<s; i++){
					if((q.sw>>i&1)==1&&maps[i][q.y][q.x]=='*'){
						virtualFloor2=1-virtualFloor2;
					}
				}
				if(ok[virtualFloor2][q.y][q.x]){
					next.add(q);
				}
			}
			for(P q : next){
				if(d[p.y][p.x][p.floor][p.sw]+1<d[q.y][q.x][q.floor][q.sw]){
					d[q.y][q.x][q.floor][q.sw]=d[p.y][p.x][p.floor][p.sw]+1;
					que.offer(q);
				}
			}
		}
		int min=INF;
		for(int sw=0; sw<1<<s; sw++){
			int virtualFloor=0;
			for(int i=0; i<s; i++){
				if((sw>>i&1)==1&&maps[i][yg][xg]=='*'){
					virtualFloor=1-virtualFloor;
				}
			}
			if(virtualFloor==0){
				min=min(min, d[yg][xg][0][sw]);
			}else{
				min=min(min, d[yg][xg][1][sw]);
			}
		}
		println((min<INF?min:-1)+"");
	}

	class P{
		int x, y, floor, sw;

		P(int x, int y, int floor, int sw){
			this.x=x;
			this.y=y;
			this.floor=floor;
			this.sw=sw;
		}
	}

	void println(String s){
		System.out.println(s);
	}

	public static void main(String[] args){
		new Main().run();
	}
}