Under construction

Still being built. Things may change

Regular Expression

Time limit1sMemory limit1024 MB

Summary
For each string A, find the minimum length of a regular expression that matches A and how many expressions reach that length.
Level

Medium4 of 10

Topics
Implementation, String
Solved
No attempts yet

Problem

Grammy has recently been interested in regular expressions, focusing on cases where the alphabet consists of the characters 'a' to 'z'. Today she asks NIO some questions. Each question gives a string AA and asks for the minimum length of an expression that matches AA under the matching rules, and the number of expressions of that minimum length.

To learn the detailed rules for how regular expressions match strings, see https://en.wikipedia.org/wiki/Regular_expression.

This problem only considers the characters 'a' to 'z' and the special characters '.', '?', '*', '+', '|', '(', and ')'. The asterisk, question mark, and plus sign have the highest priority, then concatenation, then alternation. Parentheses change the priority. For example, "a(b|c)" matches "ab" and "ac". Parentheses may be omitted when they do not change the priority. For example, "(ab)c" can be written as "abc", and "a|(b(c*))" can be written as "a|bc*".

Matching examples:

  • Alternation: "gray|grey" matches "gray" or "grey".
  • Question mark: "colou?r" matches both "color" and "colour".
  • Asterisk: "ab*c" matches "ac", "abc", "abbc", "abbbc", and so on.
  • Plus sign: "ab+c" matches "abc", "abbc", "abbbc", and so on, but not "ac".
  • Wildcard: "a.b" matches an "a", then any single character, then "b". "a.*b" matches an "a", then a "b" at some later point. "ab" matches "a.*b" but not "a.b".
  • Concatenation: let R=R = "(ab|c)", which matches {"ab", "c"}, and S=S = "(d|ef)", which matches {"d", "ef"}. Then RS=RS = "((ab|c)(d|ef))" matches {"abd", "abef", "cd", "cef"}.

Input

The input has only one test case.

The first line contains an integer QQ (1≤Q≤100 0001\leq Q\leq 100\,000), the number of questions. Each of the next QQ lines contains one string AA made of lowercase English letters (1≤∣A∣≤200 0001\leq |A|\leq 200\,000). It is guaranteed that ∑∣A∣≤1 000 000\sum |A|\leq 1\,000\,000.

Output

For each question, print one line with two integers. The first is the minimum length of a matching expression, and the second is the number of matching expressions of that length. Print both values modulo 998 244 353998\,244\,353, since the answers may be very large.

Examples1

  1. Example 1

    Input
    2
    a
    ab
    
    Expected output
    1 2
    2 6