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.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.