Associated Course(s):
Must Do Interview Preparation

Given an array of strings **A[]**, determine if the strings can be chained together to form a circle. A

string **X **can be chained together with another string **Y **if the last character of **X **is same as first

character of **Y. **If every string of the array can be chained, it will form a circle.

**For eg** for the array arr[] = {"for", "geek", "rig", "kaf"} the answer will be Yes as the given strings can be chained as "for", "rig", "geek" and "kaf"

**Input**

The first line of input contains an integer **T **denoting the number of test cases. Then **T **test cases

follow.

The first line of each test case contains a positive integer **N**, denoting the size of the array.

The second line of each test case contains a **N **space seprated strings, denoting the elements of the

array **A[]**.

**Output**

If chain can be formed, then print **1**, else print **0**.

**Constraints**

1 <= T** **<= 100

0 <= N** **<= 30

0 <= A[i] <= 10

**Examples
Input**

2

3

abc bcd cdf

4

ab bc cd da

**Output**

0

1

**Explanation:
Testcase 1:** Only strings "abc" and "cdf" follows the pattern chaining strings together but not "bcd" as none of other two strings begin with the character 'd'. Thus formation of circle of strings fails.

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

the_coder95 | 465 |

thanuvinu94 | 388 |

adityasuman2025 | 305 |

Senbonzakura_Kageyoshi | 297 |

PrakharBansal1 | 274 |

the_coder95 | 1691 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 1064 |

samay_kam_hai | 724 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.