import java.util.Scanner;

public class TrailTraining
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int tests = Integer.parseInt(sc.nextLine());
        for (int testNum = 1; testNum <= tests; ++testNum)
        {
            // System.out.println("path = " + path);
            if (testNum != 1)
                System.out.println();
            System.out.println("Test #" + testNum + ":");
            String path = shrink(sc.nextLine());
            int trainingPlanNum = Integer.parseInt(sc.nextLine());
            while (trainingPlanNum-- > 0)
            {
                String plan = sc.nextLine();
                //System.out.println("plan = " + plan);
                int cnt = 0, newIndex;
                for (int index = 0; (newIndex = path.indexOf(plan, index)) != -1; index = newIndex + plan.length())
                    ++cnt;
                System.out.println(plan + ": " + cnt);
            }
        }
    }

    public static String shrink(String s)
    {
        StringBuilder result = new StringBuilder();
        if (s != null && !s.isEmpty())
        {
            char first = s.charAt(0);
            for (int i = 1; i < s.length(); ++i)
                if (s.charAt(i) != first)
                {
                    result.append(first);
                    first = s.charAt(i);
                }
            result.append(first);
        }
        return result.toString();
    }
}
