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:
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) rep(_,v.size())cout<<" "<<v[_];cout<<endl;
#define show2d(v) rep(_,v.size()){rep(__,v[_].size())cout<<" "<<v[_][__];cout<<endl;}cout<<endl;
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 1e18+10 : 1e9+10;
int dx[]={0,1,0,-1,1,1,-1,-1};
int dy[]={1,0,-1,0,1,-1,1,-1};//RDLU
int dp[55][55][2][1333];
int bef[1030][55][55];
signed main(){
	rep(i,55)rep(j,55)rep(q,2)rep(k,1333)dp[i][j][q][k]=inf;
	
	int n,m;
	cin>>m>>n;
	vs in(n);
	rep(i,n)cin>>in[i];
	
	vector<vs>ini(2,in);
	
	rep(i,n)rep(j,m){
		if(in[i][j]=='%')ini[1][i][j]='#';
		if(in[i][j]=='_')ini[1][i][j]='#';
		if(in[i][j]=='^')ini[0][i][j]='#';
		if(islower(in[i][j]))ini[1][i][j]='#';
		if(isupper(in[i][j])){
			ini[0][i][j]='#';
			ini[1][i][j]=tolower(in[i][j]);
		}
		if(in[i][j]=='&')ini[1][i][j]='#';
	}
	
	int q;
	cin>>q;
	vector<vs>w(q,vs(n));
	rep(i,q)rep(j,n)cin>>w[i][j];
	
	queue<tp>que;
	rep(i,n)rep(j,m)if(in[i][j]=='%'){
		que.push(tp(i,j,0,0));
		dp[i][j][0][0]=0;
	}
	
	rep(C,1<<q){
		rep(c,10)if(C&1<<c)rep(i,n)rep(j,m)if(w[c][i][j]=='*')
			bef[C][i][j]^=1;
	}
	
	int out=inf;
	while(!que.empty()){
		int x,y,f,s;
		tie(x,y,f,s)=que.front();
		// cout<<x<<" "<<y<<" "<<f<<" "<<s<<" "<<dp[x][y][f][s]<<endl;
		que.pop();
		
		
		vector<vs>now(2,vs(3,string(3,'a')));
		// show2d(now);
		rep(i,2)rep(j,3)rep(k,3)now[i][j][k]=ini[i][x+j-1][y+k-1];
		// show2d(now);
		rep(i,3)rep(j,3)if(bef[s][x+i-1][y+j-1]){
			swap(now[0][i][j],now[1][i][j]);
		}
		if(now[f][1][1]=='&'){
			out=dp[x][y][f][s];
			break;
		}
		rep(k,4){
			int nx=x+dx[k];
			int ny=y+dy[k];
			if(now[f][dx[k]+1][dy[k]+1]=='#')continue;
			if(dp[x][y][f][s]+1<dp[nx][ny][f][s]){
				dp[nx][ny][f][s]=dp[x][y][f][s]+1;
				que.push(tp(nx,ny,f,s));
			}
		}
		if(isalpha(now[f][1][1])){
			int ns=s^1<<(now[f][1][1]-'a');
			int nf=f;
			if(w[now[f][1][1]-'a'][x][y]=='*')nf^=1;
			if(dp[x][y][f][s]+1<dp[x][y][nf][ns]){
				dp[x][y][nf][ns]=dp[x][y][f][s]+1;
				que.push(tp(x,y,nf,ns));
			}
		}
		if(now[f][1][1]=='|'){
			int nf=f^1;
			if(dp[x][y][f][s]+1<dp[x][y][nf][s]){
				dp[x][y][nf][s]=dp[x][y][f][s]+1;
				que.push(tp(x,y,nf,s));
			}
		}
	}
	if(out==inf)out=-1;
	cout<<out<<endl;
}