← Home
package main

import (
	"bufio"
	"bytes"
	"io"
	"os"
	"sort"
	"strconv"
)

type FastInput struct {
	data []byte
	pos  int
}

func (in *FastInput) nextInt() int {
	n := len(in.data)
	for in.pos < n && in.data[in.pos] <= ' ' {
		in.pos++
	}
	sign := 1
	if in.data[in.pos] == '-' {
		sign = -1
		in.pos++
	}
	val := 0
	for in.pos < n {
		c := in.data[in.pos]
		if c < '0' || c > '9' {
			break
		}
		val = val*10 + int(c-'0')
		in.pos++
	}
	return val * sign
}

type Pair struct {
	val int
	idx int
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	in := FastInput{data: data}
	t := in.nextInt()

	var out bytes.Buffer

	for ; t > 0; t-- {
		n := in.nextInt()
		p := in.nextInt()

		a := make([]int, n)
		ps := make([]Pair, n)
		for i := 0; i < n; i++ {
			a[i] = in.nextInt()
			ps[i] = Pair{val: a[i], idx: i}
		}

		sort.Slice(ps, func(i, j int) bool {
			return ps[i].val < ps[j].val
		})

		used := make([]bool, n-1)
		components := n
		var ans int64

		for _, pr := range ps {
			if pr.val >= p {
				break
			}
			v := pr.val
			i := pr.idx

			for j := i - 1; j >= 0; j-- {
				if used[j] || a[j]%v != 0 {
					break
				}
				used[j] = true
				components--
				ans += int64(v)
			}

			for j := i; j < n-1; j++ {
				if used[j] || a[j+1]%v != 0 {
					break
				}
				used[j] = true
				components--
				ans += int64(v)
			}
		}

		ans += int64(components-1) * int64(p)
		out.WriteString(strconv.FormatInt(ans, 10))
		out.WriteByte('\n')
	}

	w := bufio.NewWriterSize(os.Stdout, 1<<20)
	w.Write(out.Bytes())
	w.Flush()
}