← Home
package main

import (
	"bufio"
	"fmt"
	"os"
)

type AC struct {
	next []int
	link []int
	out  []int64
	size int
}

func NewAC() *AC {
	ac := &AC{
		next: make([]int, 26),
		link: make([]int, 1),
		out:  make([]int64, 1),
		size: 1,
	}
	for i := 0; i < 26; i++ {
		ac.next[i] = -1
	}
	return ac
}

func (ac *AC) add(s []byte) {
	u := 0
	for _, ch := range s {
		c := int(ch - 'a')
		idx := u*26 + c
		if ac.next[idx] == -1 {
			ac.next = append(ac.next, make([]int, 26)...)
			for i := ac.size * 26; i < (ac.size+1)*26; i++ {
				ac.next[i] = -1
			}
			ac.link = append(ac.link, 0)
			ac.out = append(ac.out, 0)
			ac.next[idx] = ac.size
			ac.size++
		}
		u = ac.next[idx]
	}
	ac.out[u]++
}

func (ac *AC) build() {
	q := make([]int, 0)
	for c := 0; c < 26; c++ {
		idx := c
		v := ac.next[idx]
		if v == -1 {
			ac.next[idx] = 0
		} else {
			ac.link[v] = 0
			q = append(q, v)
		}
	}
	for head := 0; head < len(q); head++ {
		v := q[head]
		baseV := v * 26
		linkV := ac.link[v] * 26
		for c := 0; c < 26; c++ {
			u := ac.next[baseV+c]
			if u != -1 {
				ac.link[u] = ac.next[linkV+c]
				ac.out[u] += ac.out[ac.link[u]]
				q = append(q, u)
			} else {
				ac.next[baseV+c] = ac.next[linkV+c]
			}
		}
	}
}

func (ac *AC) process(s []byte) []int64 {
	res := make([]int64, len(s))
	u := 0
	for i, ch := range s {
		c := int(ch - 'a')
		u = ac.next[u*26+c]
		res[i] = ac.out[u]
	}
	return res
}

func reverseBytes(b []byte) []byte {
	n := len(b)
	r := make([]byte, n)
	for i := 0; i < n; i++ {
		r[i] = b[n-1-i]
	}
	return r
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var t string
	if _, err := fmt.Fscan(in, &t); err != nil {
		return
	}
	var n int
	fmt.Fscan(in, &n)

	acF := NewAC()
	acR := NewAC()

	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(in, &s)
		b := []byte(s)
		acF.add(b)
		acR.add(reverseBytes(b))
	}

	acF.build()
	acR.build()

	tb := []byte(t)
	m := len(tb)

	endCounts := acF.process(tb)

	rtb := reverseBytes(tb)
	endCountsRev := acR.process(rtb)

	startCounts := make([]int64, m)
	for i := 0; i < m; i++ {
		startCounts[i] = endCountsRev[m-1-i]
	}

	var ans int64 = 0
	for k := 0; k+1 < m; k++ {
		ans += endCounts[k] * startCounts[k+1]
	}

	out := bufio.NewWriter(os.Stdout)
	fmt.Fprintln(out, ans)
	out.Flush()
}