#C3478. Longest Common Prefix

    ID: 46909 Type: Default 1000ms 256MiB

Longest Common Prefix

Longest Common Prefix

You are given two strings. Your task is to find their longest common prefix. The longest common prefix is defined as the longest substring starting from the beginning of both strings that is identical. If the two strings do not share any common prefix (or one of them is empty), output \(-1\).

For example, given the strings geeksforgeeks and geeks, the longest common prefix is geeks. Similarly, for hello and he, it is he, but for abc and def no common prefix exists so the answer is -1.

inputFormat

The input begins with a single integer (T) (the number of test cases). Then, for each test case, there are two consecutive lines, each containing a string. These strings can be any sequence of characters; note that an empty string is allowed.

outputFormat

For each test case, print the longest common prefix on a separate line. If there is no common prefix, print (-1).## sample

4
geeksforgeeks
geeks
hello
he
abc
def
abcd
geeks

he -1 -1

</p>