During the loading of the game "Dungeons and Candies" you are required to get descriptions of k levels from the server. Each description is a map of an n × m checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same.

<image>

When you transmit information via a network, you want to minimize traffic — the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level A:

  1. You can transmit the whole level A. Then you need to transmit n·m bytes via the network. 
  2. You can transmit the difference between level A and some previously transmitted level B (if it exists); this operation requires to transmit dA, B·w bytes, where dA, B is the number of cells of the field that are different for A and B, and w is a constant. Note, that you should compare only the corresponding cells of levels A and B to calculate dA, B. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other. 



Your task is to find a way to transfer all the k levels and minimize the traffic.

Input

The first line contains four integers n, m, k, w (1 ≤ n, m ≤ 10; 1 ≤ k, w ≤ 1000). Then follows the description of k levels. Each level is described by n lines, each line contains m characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters.

Output

In the first line print the required minimum number of transferred bytes.

Then print k pairs of integers x1, y1, x2, y2, ..., xk, yk, describing the way to transfer levels. Pair xi, yi means that level xi needs to be transferred by way yi. If yi equals 0, that means that the level must be transferred using the first way, otherwise yi must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels yi and xi to transfer level xi. Print the pairs in the order of transferring levels. The levels are numbered 1 through k in the order they follow in the input.

If there are multiple optimal solutions, you can print any of them.

Examples

Input

2 3 3 2
A.A
...
A.a
..C
X.Y
...


Output

14
1 0
2 1
3 1


Input

1 1 4 1
A
.
B
.


Output

3
1 0
2 0
4 2
3 0


Input

1 3 5 2
ABA
BBB
BBA
BAB
ABB


Output

11
1 0
3 1
2 3
4 2
5 1
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.TreeMap;

public class Solution{
	
	///////////////////////////////////////////////////////////////////////////
	static class FastScanner{
		BufferedReader s;
		StringTokenizer st;
		
		public FastScanner(){
			st = new StringTokenizer("");
			s = new BufferedReader(new InputStreamReader(System.in));
		}
		
		public FastScanner(File f) throws FileNotFoundException{
			st = new StringTokenizer("");
			s = new BufferedReader (new FileReader(f));
		}
		
		public int nextInt() throws IOException{
			if(st.hasMoreTokens())
				return Integer.parseInt(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextInt();
			}
		}
		
		public double nextDouble() throws IOException{
			if(st.hasMoreTokens())
				return Double.parseDouble(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextDouble();
			}
		}
		
		public long nextLong() throws IOException{
			if(st.hasMoreTokens())
				return Long.parseLong(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextLong();
			}
		}
		
		public String nextString() throws IOException{
			if(st.hasMoreTokens())
				return st.nextToken();
			else{
				st = new StringTokenizer(s.readLine());
				return nextString();
			}
			
		}
		public String readLine() throws IOException{
			return s.readLine();
		}
		
		public void close() throws IOException{
			s.close();
		}
		
	}
	
	////////////////////////////////////////////////////////////////////
	//		Number Theory
	
	public static int divisor(int x){
	    int limit = x;
	    int numberOfDivisors = 0;

	    for (int i=1; i < limit; ++i) {
	        if (x % i == 0) {
	            limit = x / i;
	            if (limit != i) {
	                numberOfDivisors++;
	            }
	            numberOfDivisors++;
	        }
	    }

	    return numberOfDivisors;
    }
	
	
	public static long gcd(long a, long b){
		return b == 0 ? a : gcd(b,a%b);
	}
	
  
	public static long lcm(long a,long b, long c){
		return lcm(lcm(a,b),c);
	}
	
	public static long lcm(long a, long b){
		return a*b/gcd(a,b);
	}
	
	
	////////////////////////////////////////////////////////////////////
	
	
//	  private static  	FastScanner s = new FastScanner(new File("input.txt"));
//	  private static PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
	  private static FastScanner s = new FastScanner();   
      private static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
//      private static Scanner s = new Scanner(System.in);
	@SuppressWarnings("unused")
	private static int[][] states = { {-1,0} , {1,0} , {0,-1} , {0,1} };
	
      
    //////////////////////////////////////////////////////////////////// 
      
     
    public static void main(String[] args) throws IOException{
 		Solution so = new Solution();
 		so.solve();
 	}
    
    ////////////////////////////////////////////////////////////////////
     
    
    
    void solve() throws IOException{
    	
		int n = s.nextInt();
		int m = s.nextInt();
		int k = s.nextInt();
		int w = s.nextInt();
		String[] s1 = new String[k];
		
		for(int i=0;i<k;i++){
			s1[i] = "";
			for(int j=0;j<n;j++)
				s1[i] += s.nextString();
		}
		
		for(int i=1;i<=k;i++){
			graph.add(new Node(0,i,n*m));
			for(int j=i+1;j<=k;j++){
				graph.add(new Node(i,j,dis(s1[i-1],s1[j-1])*w));
			}
		}
		
		Collections.sort(graph);
		
		int cost = 0;
		
		sub = new subset[k+1];
		for(int i=0;i<=k;i++)
			sub[i] = new subset(i,0);
		
		for(Node node : graph){
			
	//		System.out.println(node.from+" "+node.to+" "+node.cost);
			int x = find(node.from);
			int y = find(node.to);
			
			if(x != y){
				union(x,y);
				cost += node.cost;
				ans.add(node);
			}
			
		}
		
		Collections.sort(ans, new Comparator<Node>(){
			@Override
			public int compare(Node node1, Node node2) {
				// TODO Auto-generated method stub
				return Integer.compare(node1.from, node2.from);
			}	
		});
		
		ww.println(cost);
		
		boolean[] vis = new boolean[k+1];
		int i = 0;
		vis[0] = true;
		
		while(ans.size() > 0){
			
			Node node = ans.get(i);
			if(!vis[node.to] && !vis[node.from])
				i++;
			else{
				
				if(!vis[node.to] && vis[node.from])
					ww.println(node.to+" "+node.from);
				else
					ww.println(node.from+" "+node.to);
				vis[node.to] = true;
				vis[node.from] = true;
				ans.remove(i);
				i = 0;
			}
			
		}
		
    	s.close();
 	    ww.close(); 
    
     }
    
    int dis(String ss, String sr){
		int len = ss.length();
		int res = 0;
		for(int i=0;i<len;i++){
			if(ss.charAt(i) != sr.charAt(i))
				res++;
		}
		return res;
	}
    
    ArrayList<Node> graph = new ArrayList<Node>();
	ArrayList<Node> ans = new ArrayList<Node>();
	subset[] sub;
	
	static class Node implements Comparable<Node>{
		
		int from;
		int to;
		int cost;
		public Node(int from, int to, int cost){
			this.from = from;
			this.to = to;
			this.cost = cost;
		}
		@Override
		public int compareTo(Node node) {
			return Integer.compare(cost, node.cost);
		}
	}
	
	static class subset{
		int parent;
		int rank;
		public subset(int parent,int rank){
			this.parent = parent;
			this.rank = rank;
		}
	}
	
	int find(int x){
		
		if(sub[x].parent != x)
			return find(sub[x].parent);
		else return x;
		
	}
	
	void union(int x,int y){
		
		int xx = find(x);
		int yy = find(y);
		
		if(sub[xx].rank < sub[yy].rank)
			sub[xx].parent = yy;
		else if(sub[xx].rank > sub[yy].rank)
			sub[yy].parent = xx;
		else{
			sub[xx].parent = yy;
			sub[yy].rank++;
		}
		
	}
	
}
