Given a string S which contains only small letters. The task is to make a square matrix from string S. Then perform the following operations.

1. In each column delete the characters which occur more than one.

2. Column-wise sort the remaining characters in order abcd....wxyz. But print them in order azbycxdw....

For example, after operation 1 if the remaining characters in a column are cedjki then after sorting they become cdeijk , now when printed in the given order the output will be ckdjei .

**Input:**

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains a string S as input.

**Output:**

For each test case, in a new line print the above operations result. If there is no character left in the matrix after operation 1 then print "0".

**Constraints:**

1<=T<=300

1<=S<=1000

**Example:**

**Input:**

3

adgkbdhlceilcfjm

aaaabbbcccdd

abcabcabc

**Output:**

abefgjhikm

acbacbadbadc

0

**Explanation:**

**In 1 ^{st} test case**

Matrix will be

a d g k

b d h l

c e i l

c f j m

In 1st column 'c', in 2nd column 'd' and in 4th column 'l' has occurred more than once. So after deleting them and sorting the remaining characters the matrix become

a - g k

b - h -

- e i -

- f j m

So after operation 2 we get 'ab' from 1st column, 'ef' from 2nd column, 'gjhi' from 3rd column and 'km' from 4th column. Appending all of them we get the output as "abefgjhikm".

**In 3 ^{rd} test case **

Matrix will be

a b c

a b c

a b c

So the output will be 0.

Author: arun03

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.

agness | 122 |

aman1931998 | 102 |

Survivor | 100 |

uchitnitk | 96 |

lonecoder | 95 |

KshatriyaYash | 1066 |

nikhil_sojan | 1032 |

lonecoder | 1030 |

mazumderrohit8 | 899 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4492 |

Quandray | 4444 |

Login to report an issue on this page.