import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	static Map<Character, Pair> map;
	
//OBJECTO
public static class Objecto implements Comparable<Objecto>{

	String letters;
	Integer distance;
	
	public Objecto(String letters, Integer distance){
		this.letters = letters;
		this.distance = distance;
	}

	@Override
	public int compareTo(Objecto o) {
		// TODO Auto-generated method stub
		
		if(this.distance.intValue() == o.distance.intValue()){
			return this.letters.compareTo(o.letters);
		}
		
		return this.distance.compareTo(o.distance);
	}
	
	@Override
	public String toString(){
		return this.letters + " " + this.distance; 
	}
}

//PAIR

public static class Pair {
	int x;
	int y;
	
	public Pair(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	public int distanceTo(Pair another){
		return Math.abs(this.x - another.x) + Math.abs(this.y - another.y);
	}
}


	//MAIN
	public static void main(String[] args){
		
		map = new HashMap<Character, Pair>();
		
		map.put('q', new Pair(0, 0));
		map.put('w', new Pair(0, 1));
		map.put('e', new Pair(0, 2));
		map.put('r', new Pair(0, 3));
		map.put('t', new Pair(0, 4));
		map.put('y', new Pair(0, 5));
		map.put('u', new Pair(0, 6));
		map.put('i', new Pair(0, 7));
		map.put('o', new Pair(0, 8));
		map.put('p', new Pair(0, 9));
		
		map.put('a', new Pair(1, 0));
		map.put('s', new Pair(1, 1));
		map.put('d', new Pair(1, 2));
		map.put('f', new Pair(1, 3));
		map.put('g', new Pair(1, 4));
		map.put('h', new Pair(1, 5));
		map.put('j', new Pair(1, 6));
		map.put('k', new Pair(1, 7));
		map.put('l', new Pair(1, 8));
	
		map.put('z', new Pair(2, 0));
		map.put('x', new Pair(2, 1));
		map.put('c', new Pair(2, 2));
		map.put('v', new Pair(2, 3));
		map.put('b', new Pair(2, 4));
		map.put('n', new Pair(2, 5));
		map.put('m', new Pair(2, 6));
		
		Scanner in = new Scanner(System.in);
		int tests = in.nextInt();
		PriorityQueue<Objecto> q = new PriorityQueue<Objecto>();
		String[] tokens = null;
		String line = null;
		String word = "";
		String spellCheckWord = "";
		int spellCheckWords = 0, j;
		
		for(int i = 0; i <= tests; i += 1){
			line = in.nextLine();
			tokens = line.split(" ");
			
			if(tokens.length != 2){
				continue;
			}
			
			word = tokens[0];
			spellCheckWords = Integer.parseInt(tokens[1]);
			
			for(j = 0; j < spellCheckWords; j += 1){
				spellCheckWord = in.nextLine();
				
				q.add(getObjecto(word, spellCheckWord));
			}
			
			while(!q.isEmpty()){
				System.out.println(q.poll());
			}
		}
		
		in.close();
	}
	
	static Objecto getObjecto(String word, String spellCheckWord){
		int distance = 0;
		char[] charsOfWord = word.toCharArray(), charsOfSpellCheckWord = spellCheckWord.toCharArray();
		
		for(int i = 0; i < charsOfWord.length; i += 1){
			distance += map.get(charsOfWord[i]).distanceTo(map.get(charsOfSpellCheckWord[i]));
		}
		
		return new Objecto(spellCheckWord, distance);
	}
}