Under construction

Still being built. Things may change

Similar Rhyme Game

Interview

Time limit1sMemory limit512 MB

Summary
Given n distinct words, pair them up so that each pair shares a common suffix of length at least F, maximizing the number of pairs.
Level

Medium5 of 10

Topics
Trie, Greedy, String, DFS
Solved
No attempts yet

Problem

Alice enjoys a word game in which she combines English words to make rhymes. Bob also wants to make rhymes, but he is young and does not know many words, so Alice proposes the "Similar Rhyme Game."

First, write nn distinct words of length LL consisting only of uppercase English letters on paper. For convenience, call these words W1,W2,…,WnW_1, W_2, \dots, W_n.

When comparing a pair of words WiW_i and WjW_j, if the length of their longest common suffix is at least FF, the two words are said to form a "similar rhyme." Such a pair of words is called a "similar rhyme pair."

The "Similar Rhyme Game" is a game of forming as many "similar rhyme pairs" as possible from the given words. Each word may belong to at most one similar rhyme pair.

For example, let n=4n = 4 and L=4L = 4, with the four words W1=W_1 = "WALK", W2=W_2 = "TALK", W3=W_3 = "MILK", W4=W_4 = "BULK".

  • If F≤2F \le 2, pairing the words into any two pairs gives similar rhyme pairs, so 22 pairs can be found. For example, ("WALK", "BULK") and ("TALK", "MILK").
  • If F=3F = 3, pairing ("WALK", "TALK") forms one similar rhyme pair, but there is no way to form 22 or more.
  • If F≥4F \ge 4, there is no way to form a similar rhyme pair.

Given FF and nn words of length LL as input, find the maximum number of "similar rhyme pairs" Bob can form.

Input

The first line gives the number of test cases TT.

The input for each test case is given over two lines. The first line gives nn, LL, and FF, separated by spaces. The second line gives nn strings W1,W2,…,WnW_1, W_2, \dots, W_n of length LL consisting only of uppercase English letters, separated by spaces.

Output

For each test case, print the answer on its own line.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤F≤L≤101 \le F \le L \le 10
  • 2≤n≤5002 \le n \le 500
  • Each string WiW_i consists only of uppercase English letters.
  • The nn strings given in each test case are distinct.

Hint

Longest common suffix: the longest common suffix of two strings, that is, the common suffix with the greatest length.

Examples1

  1. Example 1

    Input
    5
    4 4 2
    WALK TALK MILK BULK
    4 4 3
    WALK TALK MILK BULK
    4 4 4
    WALK TALK MILK BULK
    6 3 1
    AXB BCC DEE XBB YCC ZEE
    6 3 2
    AXB BCC DEE XBB YCC ZEE
    
    Expected output
    2
    1
    0
    3
    2