---
{"category_name":"easy","problem_code":"SSTRPREF2","problem_name":"Prefix as a Substring 2","problemComponents":{"constraints":"- $1 \\leq T \\leq 10^5$\n- $1 \\leq |X|, |S_1|, |S_2| \\leq 10^6$\n- Sum of $|X|$ over all test cases doesn\u0027t exceed $10^6$.\n- Sum of $|S_1|$ over all test cases doesn\u0027t exceed $10^6$.\n- Sum of $|S_2|$ over all test cases doesn\u0027t exceed $10^6$.","constraintsState":true,"subtasks":"- 30 points : $1 \\leq R \\leq 10000$\n- 70 points : $1 \\leq R \\leq 10^9$\n","subtasksState":false,"inputFormat":"- First line of the input contains $T$, the number of test cases. Then the test cases follow.\n- Each test case contains three lines, string $S_1$ in the first line, string $S_2$ in the second line and string $X$ in the third line. All strings consist of only lowercase Latin letters.\n","inputFormatState":true,"outputFormat":"For each test case, print a single line containing one integer denoting the number of distinct ordered pairs $(P,Q)$ that satisfy the above conditions.\n","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"3\nab\nbc\nabc\naa\nbb\nab\naab\nacb\nbcaabacbc","output":"7\n4\n11","explanation":"**Test case 1:**\n\nThere are $7$ distinct ordered pairs $(P,Q)$ as follows : \n\n- $P$ $=$ \u0022\u0022 , $Q$ $=$ \u0022\u0022 (empty string)\n- $P$ $=$ \u0022\u0022 , $Q$ $=$ $b$\n- $P$ $=$ \u0022\u0022 , $Q$ $=$ $bc$\n- $P$ $=$ $a$ , $Q$ $=$ \u0022\u0022\n- $P$ $=$ $a$ , $Q$ $=$ $b$\n- $P$ $=$ $a$ , $Q$ $=$ $bc$\n- $P$ $=$ $ab$ , $Q$ $=$ \u0022\u0022\n\n\n**Test case 2:**\n\nThere are $4$ distinct ordered pairs as follows : \n\n- $P$ $=$ \u0022\u0022 , $Q$ $=$ \u0022\u0022\n- $P$ $=$ \u0022\u0022 , $Q$ $=$ $b$\n- $P$ $=$ $a$ , $Q$ $=$ \u0022\u0022\n- $P$ $=$ $a$ , $Q$ $=$ $b$\n\n","isDeleted":false}}},"video_editorial_url":"https://youtu.be/_bgHPwyrDnA","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"nishant_adm","problem_tester":"","date_added":"10-10-2021","tags":{"0":"knuth","1":"medium","2":"nishant_adm","3":"snckql21","4":"z"},"problem_difficulty_level":"Unavailable","best_tag":"Knuth Morris Pratt Algorithm","editorial_url":"https://discuss.codechef.com/problems/SSTRPREF2","time":{"view_start_date":1634668200,"submit_start_date":1634668200,"visible_start_date":1634668200,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=SSTRPREF2","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problem statements in [Mandarin Chinese](https://www.codechef.com/download/translated/SNCKQL21/mandarin/SSTRPREF2.pdf), [Russian](https://www.codechef.com/download/translated/SNCKQL21/russian/SSTRPREF2.pdf), and [Vietnamese](https://www.codechef.com/download/translated/SNCKQL21/vietnamese/SSTRPREF2.pdf) as well.
Given $3$ strings $S_1$, $S_2$ and $X$, find the number of distinct [ordered pairs](https://en.wikipedia.org/wiki/Ordered_pair) of strings $(P,Q)$
such that :
- String $P + Q$ is a substring of $X$.
- String $P$ is some prefix of $S_1$ (possibly empty).
- String $Q$ is some prefix of $S_2$ (possibly empty).
A **substring** of a string is a contiguous subsequence of that string. For example, "chef" is a substring of "codechef", but "def" is not. Also, an empty string is a substring of any string.
A **prefix** of a string $S$ is a substring of $S$ that occurs at the beginning of $S$. For example, "code" is a prefix of "codechef", but "chef" is not. Also, an empty string is a prefix of any string.
**Note:** Since the input is large, prefer using fast input-output methods.
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>