You are given a keyboard that consists of $26$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.

You have to type the word $s$ on this keyboard. It also consists only of lowercase Latin letters.

To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.

Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.

For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $8$, $5$, $12$ and $15$, respectively. Therefore, it will take $|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$ units of time to type the word "hello".

Determine how long it will take to print the word $s$.


-----Input-----

The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.

The next $2t$ lines contain descriptions of the test cases.

The first line of a description contains a keyboard — a string of length $26$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard.

The second line of the description contains the word $s$. The word has a length from $1$ to $50$ letters inclusive and consists of lowercase Latin letters.


-----Output-----

Print $t$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $s$ on the given keyboard.


-----Examples-----

Input
5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba
Output
13
0
68
0
74


-----Note-----

None

Source: codeforces | Difficulty: EASY
Tags: String algorithms

Implementation


Solutions:
t = int(input())
for m in range(t):
	n = 26
	key = input()
	word = list(input())
	l = []
	result = 0
	for i in range(len(word)):
		for j in range(26):
			if word[i] == key[j]:
				k = j + 1
				l.append(k)
	for x in range(len(l) - 1):
		result += abs(l[x] - l[x + 1])
	print(result)


num_cases = int(input())
for i in range(num_cases):
	keyboard = input()
	s = input()
	time = 0
	position = keyboard.index(s[0])
	for j in range(1, len(s)):
		next_position = keyboard.index(s[j])
		time += abs(next_position - position)
		position = next_position
	print(time)


import sys

def distance(alpha, word):
	d = {}
	c = 0
	for i in alpha:
		d[i] = c
		c += 1
	res = 0
	for i in range(len(word) - 1):
		res += abs(d[word[i]] - d[word[i + 1]])
	return res
t = input()
for i in range(int(t)):
	alpha = input()
	word = input()
	print(distance(alpha, word))


ladwa = int(input())
for fcuk in range(ladwa):
	klh = []
	sum = 0
	shakeer = input()
	basha = input()
	for j in range(len(basha)):
		for i in range(len(shakeer)):
			if basha[j] == shakeer[i]:
				klh.append(i + 1)
	for i in range(1, len(klh)):
		sum = sum + abs(klh[i - 1] - klh[i])
	print(sum)


n = int(input())
for i in range(0, n):
	sum = 0
	q = input()
	s = input()
	for j in range(0, len(s) - 1):
		t1 = 0
		t = 0
		for k in range(0, len(q)):
			if q[k] == s[j]:
				t1 = k
			if q[k] == s[j + 1]:
				t = k
		sum = sum + abs(t1 - t)
	print(sum)


def tinh_vi_tri(keyboard: str, key: str):
	return keyboard.index(key) + 1

def giai_quyet_van_de():
	keyboard = input()
	key = input()
	t = 0
	for i in range(2, len(key) + 1):
		a = tinh_vi_tri(keyboard, key[i - 1])
		b = tinh_vi_tri(keyboard, key[i - 2])
		t += abs(a - b)
	print(t)
t = int(input())
for i in range(t):
	giai_quyet_van_de()


for _ in range(int(input())):
	s = input()
	wrd = input()
	count = 0
	for i in range(len(wrd)):
		try:
			count += abs(s.index(wrd[i]) - s.index(wrd[i + 1]))
		except:
			print(count)
			break


for _ in range(int(input())):
	keyboard = input()
	word = input()
	steps = 0
	for i in range(1, len(word)):
		steps += abs(keyboard.find(word[i]) - keyboard.find(word[i - 1]))
	print(steps)


n = int(input())
for _ in range(n):
	cl = input()
	instr = input()
	ln = 0
	st = instr[0]
	stIn = cl.index(st)
	for i in instr:
		if not i == st:
			iIn = cl.index(i)
			ln += abs(stIn - iIn)
			st = i
			stIn = iIn
	print(ln)


def findMinDistance(string, word_seq):
	counter = 0
	word_freq = {}
	for c in word_seq:
		word_freq[c] = counter
		counter += 1
	a = word_freq[string[0]]
	b = None
	mindist = 0
	for i in range(1, len(string)):
		b = word_freq[string[i]]
		mindist += abs(a - b)
		a = b
	return mindist
N = int(input())
for _ in range(N):
	word_seq = input()
	string = input()
	print(findMinDistance(string, word_seq))


t = int(input())
while t > 0:
	alpbts = str(input())
	word = str(input())
	s = 0
	for i in range(len(word) - 1):
		s += abs(alpbts.index(word[i + 1]) - alpbts.index(word[i]))
	print(s)
	t -= 1


for _ in range(int(input())):
	keyboard = input().strip()
	string = input().strip()
	(prev, total) = (0, 0)
	for i in range(1, len(string)):
		total += abs(keyboard.index(string[i - 1]) - keyboard.index(string[i]))
	print(total)


N = int(input())
for x in range(N):
	abcd = input()
	word = input()
	previous = word[0]
	score = 0
	for x in word:
		score += abs(abcd.index(previous) - abcd.index(x))
		previous = x
	print(score)


keymap = [0] * 256
for _ in range(int(input())):
	(k, w) = (input(), input())
	for (i, c) in enumerate(k):
		keymap[ord(c)] = i
	print(sum((abs(keymap[ord(p)] - keymap[ord(n)]) for (p, n) in zip(w, w[1:]))))


n = int(input())
for i in range(n):
	alph = input()
	s = input()
	l = len(s)
	summa = 0
	for i in range(1, l):
		summa += abs(alph.find(s[i]) - alph.find(s[i - 1]))
	print(summa)


t = int(input())
for i in range(t):
	al = str(input())
	w = str(input())
	d = 0
	for j in range(len(w) - 1):
		c = abs(al.index(w[j]) - al.index(w[j + 1]))
		d += c
	print(d)


for _ in range(int(input())):
	a = input()
	s = input()
	t = 0
	for i in range(1, len(s)):
		t += abs(a.index(s[i - 1]) - a.index(s[i]))
	print(t)


n = int(input())
for i in range(n):
	s = str(input())
	f = str(input())
	l = list(s)
	d = list(f)
	a = []
	for i in range(len(d)):
		for j in range(0, 26):
			if d[i] == l[j]:
				a.append(j + 1)
	count = 0
	for i in range(len(a) - 1):
		c = abs(a[i + 1] - a[i])
		count += c
	print(count)


def f():
	a = input()
	d = dict()
	for i in range(26):
		d[a[i]] = i
	s = input()
	ans = 0
	p = s[0]
	for i in s[1:]:
		ans += abs(d[p] - d[i])
		p = i
	return ans
for _ in range(int(input())):
	print(f())


t = 0

def ra(nums):
	return [a for (a, b) in zip(nums, nums[1:] + [not nums[-1]]) if a != b]
for x in range(int(input())):
	a = [char for char in input()]
	b = [char for char in input()]
	b = ra(b)
	t = 0
	ans = []
	for x in b:
		ans.append(a.index(x) + 1)
	for y in range(len(ans) - 1):
		t += abs(ans[y] - ans[y + 1])
	print(t)


import math
for i in range(int(input())):
	alfavit = input()
	slovo = input()
	count = 0
	for a in range(len(slovo) - 1):
		count += math.fabs(alfavit.index(slovo[a]) - alfavit.index(slovo[a + 1]))
	print(int(count))


t = int(input())
for _ in range(t):
	y = input()
	s = input()
	x = []
	sum1 = 0
	for each in s:
		x.append(y.index(each) + 1)
	for i in range(len(x) - 1):
		sum1 += abs(x[i] - x[i + 1])
	print(sum1)


testcase = int(input())
for i in range(testcase):
	alphabet = input()
	s = input()
	time = 0
	for j in range(len(s) - 1):
		time += abs(alphabet.index(s[j]) - alphabet.index(s[j + 1]))
	print(time)


a = int(input())
for i in range(a):
	p = []
	sum = sum1 = sum2 = count = 0
	n = input()
	m = input()
	for j in n:
		sum1 = sum1 + 1
	for j in m:
		sum2 = sum2 + 1
	for j in range(sum2):
		for c in range(sum1):
			if m[j] == n[c]:
				p.append(int(c) + 1)
				sum = sum + 1
				break
	for j in range(sum - 1):
		count = count + abs(p[j + 1] - p[j])
	print(count)


for _ in range(int(input())):
	keyboard = list(input())
	s = list(input())
	time = 0
	for i in range(1, len(s)):
		time += abs(keyboard.index(s[i]) - keyboard.index(s[i - 1]))
	print(time)


def fun(s, t):
	n = len(t)
	if n == 1:
		return 0
	ans = 0
	d = {}
	for (i, ch) in enumerate(s):
		d[ch] = i
	for i in range(1, len(t)):
		ans += abs(d[t[i]] - d[t[i - 1]])
	return ans
t = int(input())
for i in range(t):
	s = input()
	t = input()
	ans = fun(s, t)
	print(ans)


for _ in range(int(input())):
	x = 0
	mp = {}
	for i in input():
		mp[i] = x
		x += 1
	s = [mp[i] for i in input()]
	print(sum((abs(s[i] - s[i + 1]) for i in range(len(s) - 1))))


t = int(input())
for i in range(t):
	k = input()
	s = input()
	cnt = 0
	if len(s) > 1:
		for j in range(1, len(s)):
			cnt += abs(k.index(s[j]) - k.index(s[j - 1]))
	print(cnt)


t = int(input())
for j in range(t):
	letters = list(input())
	word = list(input())
	ans = 0
	last = letters.index(word[0])
	for letter in word:
		for j in range(26):
			if letter == letters[j]:
				ans += abs(j - last)
				last = j
				break
	print(ans)


t = int(input())
for i in range(t):
	sx = input()
	d = {}
	for i in range(len(sx)):
		d[sx[i]] = i + 1
	s = input()
	c = 0
	curr = d[s[0]]
	for i in range(1, len(s)):
		c += abs(curr - d[s[i]])
		curr = d[s[i]]
	print(c)


t = int(input())
while t:
	t -= 1
	abc = input()
	s = input()
	dict = {}
	i = 0
	for a in abc:
		dict[a] = i
		i += 1
	result = 0
	for (x, y) in zip(s, s[1:]):
		result += abs(dict[x] - dict[y])
	print(result)


for i in range(int(input())):
	s = input()
	a = input()
	al = len(a)
	ad = 0
	for i in range(1, al):
		ad += abs(s.index(a[i]) - s.index(a[i - 1]))
	print(ad)


for _ in range(int(input())):
	alph = input()
	s = input()
	print(sum([abs(alph.index(s[i]) - alph.index(s[i - 1])) for i in range(1, len(s))]))


import math as m

def L():
	return list(map(int, input().split()))

def I():
	return int(input())

def M():
	return map(int, input().split())

def solve():
	k = input()
	dictt = {}
	for x in k:
		dictt[x] = k.index(x)
	s = input()
	ans = 0
	for i in range(len(s) - 1):
		ans += abs(dictt[s[i]] - dictt[s[i + 1]])
	print(ans)
for _ in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	a = input()
	b = input()
	ans = 0
	n = len(b)
	for i in range(n - 1):
		ans += abs(a.index(b[i]) - a.index(b[i + 1]))
	print(ans)


n = int(input())
for i in range(n):
	s = str(input())
	t = str(input())
	c = 0
	a = []
	for i in range(len(t)):
		k = t[i]
		l = i
		for i in range(len(s)):
			if k == s[i]:
				a.append(i + 1)
	for i in range(len(a) - 1):
		c += abs(a[i + 1] - a[i])
	print(c)


def linear_keyboard(keyboard, word):
	position_by_letter = {}
	for i in range(len(keyboard)):
		position_by_letter[keyboard[i]] = i
	total = 0
	for j in range(1, len(word)):
		total += abs(position_by_letter[word[j]] - position_by_letter[word[j - 1]])
	return total
for _ in range(int(input())):
	keyboard = input()
	word = input()
	print(linear_keyboard(keyboard, word))


n = int(input())
while n > 0:
	n -= 1
	alfa = input()
	string = input()
	tempo = 0
	for i in range(len(string) - 1):
		tempo += abs(alfa.index(string[i]) + 1 - (alfa.index(string[i + 1]) + 1))
	print(tempo)


for t in range(int(input())):
	s = input()
	s1 = input()
	a = 0
	for i in range(len(s1) - 1):
		a = a + abs(s.index(s1[i]) - s.index(s1[i + 1]))
	print(str(a))


def calc_time(kb, word):
	alphabet = list(kb)
	word_nums = list()
	ans = 0
	for letter in word:
		word_nums.append(alphabet.index(letter) + 1)
	if len(word_nums) >= 2:
		for i in range(1, len(word_nums)):
			ans += abs(word_nums[i] - word_nums[i - 1])
	return ans
t = int(input())
for _ in range(t):
	kb = input().strip()
	word = input().strip()
	print(calc_time(kb, word))


for i in range(int(input())):
	keys = input()
	word = input()
	list1 = []
	output = []
	out2 = []
	count = 0
	for i in keys:
		list1.append(ord(i) - 96)
	for i in word:
		if count == 0:
			out2.append(keys.index(i))
			output.append(keys.index(i))
			count += 1
		else:
			out2.append(keys.index(i))
			output.append(abs(out2[count - 1] - keys.index(i)))
			count += 1
	ans = 0
	ok = 0
	for i in output:
		if ok != 0:
			ans += i
		ok += 1
	print(ans)


def main():
	n = int(input())
	for i in range(n):
		alp = input()
		word = input()
		pr = alp.index(word[0])
		sum = 0
		for j in range(1, len(word)):
			x = alp.index(word[j])
			sum += abs(pr - x)
			pr = x
		print(sum)
main()


t = int(input())
sum = []
for i in range(t):
	yep = 0
	keyboard = ' '.join(input()).split()
	word = ' '.join(input()).split()
	for k in range(len(word) - 1):
		yep += abs(keyboard.index(word[k + 1]) - keyboard.index(word[k]))
	sum.append(yep)
for j in range(len(sum)):
	print(sum[j])


t = int(input())
for i in range(t):
	key = input()
	word = input()
	time = 0
	for j in range(len(word) - 1):
		time += abs(key.index(word[j]) - key.index(word[j + 1]))
	print(time)


import sys
line = sys.stdin.readline().strip()
nums = int(line)
for _ in range(nums):
	count = 0
	keyboard = sys.stdin.readline().strip()
	case = sys.stdin.readline().strip()
	last_offset = -1
	for (idx, s) in enumerate(case):
		offset = keyboard.find(s)
		if idx != 0:
			count += abs(offset - last_offset)
		last_offset = offset
	print(count)


import sys
input = sys.stdin.readline
T = int(input())
result = []
for _ in range(T):
	raw_keyboard = input()[:-1]
	word = input()[:-1]
	t = 0
	if len(word) == 1:
		result.append(str(0))
		continue
	key_map = {}
	for (i, k) in enumerate(raw_keyboard):
		key_map[k] = i + 1
	for i in range(len(word) - 1):
		t += abs(key_map[word[i]] - key_map[word[i + 1]])
	result.append(str(t))
print('\n'.join(result))


t = int(input())
for i in range(t):
	count = 0
	board = input()
	word = input()
	board = list(board)
	word = list(word)
	n = len(word)
	for i in range(0, n - 1):
		a = board.index(word[i]) + 1
		b = board.index(word[i + 1]) + 1
		count = count + abs(a - b)
	print(count)


import sys

def function(a, b):
	str_dict = {}
	for (i, ltr) in enumerate(a):
		str_dict[ltr] = i
	movements = 0
	for i in range(1, len(b) - 1):
		movements += abs(str_dict[b[i]] - str_dict[b[i - 1]])
	return movements

def main():
	t = int(sys.stdin.readline())
	for _ in range(t):
		a = sys.stdin.readline()
		b = sys.stdin.readline()
		res = function(a, b)
		print(res)
main()


def findp(x, y):
	t = []
	sum = 0
	for i in y:
		if i in x:
			t.append(x.index(i))
	if len(t) == 1:
		sum = 0
	else:
		for j in range(1, len(t)):
			sum = sum + abs(t[j - 1] - t[j])
	print(sum)
n = int(input())
for i in range(n):
	x = input()
	y = input()
	findp(x, y)


t = int(input())
for _ in range(t):
	alh = input()
	st = input()
	ans = 0
	for i in range(1, len(st)):
		ans += abs(alh.index(st[i]) - alh.index(st[i - 1]))
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	s1 = input()
	d = [0] * 26
	for i in range(len(s)):
		d[ord(s[i]) - ord('a')] = i
	ans = 0
	for i in range(len(s1)):
		if i != 0:
			ans += abs(d[ord(s1[i]) - ord('a')] - d[ord(s1[i - 1]) - ord('a')])
	print(ans)


n = int(input())
for i in range(0, n):
	s = input().rstrip()
	o = input().rstrip()
	x = list(s)
	y = list(o)
	L = []
	for j in range(0, len(y)):
		for k in range(0, len(x)):
			if x[k] == y[j]:
				L.append(k + 1)
				break
	ans = 0
	for j in range(0, len(L) - 1):
		ans += abs(L[j + 1] - L[j])
	print(ans)


t = int(input())
for _ in range(t):
	k = input()
	s = input()
	l = len(s)
	if l == 1:
		print(0)
		continue
	res = 0
	c_i = k.index(s[0]) + 1
	for c in range(1, l):
		i = k.index(s[c]) + 1
		res += abs(i - c_i)
		c_i = i
	print(res)


t = int(input())
for i in range(t):
	al = input()
	m = {}
	for j in range(26):
		m[al[j]] = j
	s = input()
	ans = 0
	for j in range(1, len(s)):
		ans += abs(m[s[j]] - m[s[j - 1]])
	print(ans)


def function(words, keyboards):
	time = []
	for i in range(len(words)):
		w = words[i]
		b = keyboards[i]
		c_time = 0
		if len(w) != 1:
			c_l = w[0]
			for l in w[1:]:
				c_time += abs(b[c_l] - b[l])
				c_l = l
		time.append(str(c_time))
	return '\n'.join(time)
t = int(input())
keyboards = []
words = []
for i in range(t):
	board = input()
	words.append(input())
	board_dict = {}
	i = 1
	for key in board:
		board_dict[key] = i
		i += 1
	keyboards.append(board_dict)
ans = function(words, keyboards)
print(ans)


for i in range(int(input())):
	x = input()
	y = input()
	s = 0
	if len(y) == y.count(y[0]):
		print(s)
		continue
	if len(y) == 1:
		print(s)
		continue
	for j in range(len(y) - 1):
		s += abs(x.index(y[j + 1]) - x.index(y[j]))
	print(s)


for i in range(int(input())):
	key = input()
	s = input()
	cnt = 0
	for j in range(len(s) - 1):
		cnt += abs(key.index(s[j]) - key.index(s[j + 1]))
	print(cnt)


t = int(input())
res = []
for i in range(t):
	cnt = 0
	time = 0
	dictA = {}
	pos = []
	key = input()
	s = input()
	for j in key:
		cnt += 1
		dictA[j] = cnt
	for j in s:
		pos.append(dictA[j])
	for i in range(len(pos)):
		try:
			time += abs(pos[i + 1] - pos[i])
		except:
			break
	res.append(time)
for i in res:
	print(i)


def number_finder(letter):
	for (number, alpha_letter) in zip(numbers, alphabet):
		if alpha_letter == letter:
			return number

def time_counter(alphabet, word):
	time = 0
	first = word[:-1]
	second = word[1:]
	for i in range(len(word) - 1):
		time += abs(number_finder(first[i]) - number_finder(second[i]))
	return time
n = int(input())
for i in range(n):
	alphabet = list(input())
	word = list(input())
	numbers = []
	for i in range(len(alphabet)):
		numbers.append(i + 1)
	print(time_counter(alphabet, word))


T = int(input())
while T > 0:
	T -= 1
	s = input()
	target = input()
	ans = 0
	for i in range(len(target) - 1):
		ans += abs(s.index(target[i]) - s.index(target[i + 1]))
	print(ans)


t = int(input())
for i in range(t):
	alpha = {}
	s = list(input())
	for (idx, ch) in enumerate(s):
		alpha[ch] = idx
	ans = 0
	inp = list(input())
	inp.append(inp[-1])
	for (idx, ch) in enumerate(inp[:-1]):
		ans = ans + abs(alpha[ch] - alpha[inp[idx + 1]])
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	s2 = input()
	x = 0
	count = 0
	for i in s2:
		count += abs(s.index(i) - x)
		x = s.index(i)
	print(count - s.index(s2[0]))


x = int(input())
for i in range(x):
	key = input()
	s = input()
	sum = 0
	NUM = 31
	for j in range(1, len(s)):
		sum += abs(key.index(s[j]) - key.index(s[j - 1]))
	print(sum)


import sys
input = sys.stdin.readline
output = sys.stdout.write

def main():
	tests = int(input().rstrip())
	for i in range(tests):
		keyboard = input().rstrip()
		dic_ = {keyboard[j]: j + 1 for j in range(26)}
		dist_ = 0
		word_ = input().rstrip()
		last_ = dic_[word_[0]]
		for t in word_[1:]:
			y = dic_[t]
			dist_ += abs(y - last_)
			last_ = y
		output(str(dist_))
		output('\n')
main()


def solve():
	keyboard = str(input())
	word = str(input())
	result = 0
	temp = keyboard.index(word[0])
	for i in range(1, len(word)):
		t = keyboard.index(word[i])
		result += abs(temp - t)
		temp = t
	print(result)
t = int(input())
for i in range(t):
	solve()


t = int(input())
for i in range(t):
	k = input()
	s = input()
	c = 0
	p = k.index(s[0])
	for ch in s:
		pos = k.index(ch)
		c += abs(pos - p)
		p = pos
	print(c)


for _ in range(int(input())):
	keys = input()
	inp = input()
	dkeys = {}
	for i in range(len(keys)):
		dkeys[keys[i]] = i + 1
	ans = 0
	for i in range(1, len(inp)):
		ans += abs(dkeys[inp[i]] - dkeys[inp[i - 1]])
	print(ans)


for _ in range(2 * int(input())):
	a = input()
	if _ % 2 == 0:
		d = {}
		for i in range(len(a)):
			d[a[i]] = i
	else:
		sum1 = 0
		for i in range(len(a) - 1):
			sum1 += abs(d[a[i]] - d[a[i + 1]])
		print(sum1)


import math
import bisect

def mapIn():
	return map(int, input().split())

def listIn():
	return list(map(int, input().split()))

def intIn():
	return int(input())
intMax = float('inf')
intMin = float('-inf')
d4x = [1, -1, 0, 0]
d4y = [0, 0, 1, -1]
d8x = [1, -1, 0, 0, -1, -1, 1, 1]
d8y = [0, 0, 1, -1, -1, 1, -1, 1]

def checkBit(n, i):
	return n >> i & 1
mod = int(1000000000.0 + 7)

def solve():
	a = input()
	h = {}
	c = 1
	for i in a:
		h[i] = c
		c += 1
	ans = 0
	b = input()
	if len(b) == 1:
		print(0)
		return
	for i in range(1, len(b)):
		ans += abs(h[b[i]] - h[b[i - 1]])
	print(ans)
t = int(input())
for tc in range(t):
	solve()


n = int(input())
L = []
for i in range(n):
	st = input()
	st2 = input()
	s = 0
	ind = st.index(st2[0])
	for j in range(1, len(st2)):
		ind2 = st.index(st2[j])
		s += abs(ind - ind2)
		ind = ind2
	L.append(s)
for el in L:
	print(el)


t = int(input())
while t:
	su = 0
	l = []
	s = input()
	string = input()
	for i in range(0, len(string)):
		for j in range(0, len(s)):
			if string[i] == s[j]:
				l.append(j + 1)
	for k in range(0, len(l) - 1):
		su += abs(l[k + 1] - l[k])
	print(su)
	t -= 1


t = int(input())
for i in range(t):
	alph = input()
	s = input()
	suu = 0
	for i in range(len(s) - 1):
		suu += abs(alph.index(s[i + 1]) - alph.index(s[i]))
	print(suu)


import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 5)
INF = 10 ** 18

def solve():
	keyboard = input()
	s = input()
	cur = -1
	ans = 0
	for c in s:
		if cur == -1:
			cur = keyboard.index(c)
		else:
			cur2 = keyboard.index(c)
			ans += abs(cur2 - cur)
			cur = cur2
	print(ans)
Q = int(input())
for _ in range(Q):
	solve()


for _ in range(int(input())):
	(s, m) = (input(), input())
	res = 0
	for i in range(len(m) - 1):
		j = s.index(m[i])
		res = res + abs(j - s.index(m[i + 1]))
	print(res)


a = int(input())
for i in range(a):
	ind = 0
	count = 0
	klava = input()
	line = input()
	for j in range(len(line)):
		if j == 0:
			ind = klava.index(line[0])
		else:
			count += abs(ind - klava.index(line[j]))
			ind = klava.index(line[j])
	print(count)


for i in range(int(input())):
	a = 0
	q = list(input())
	A = input()
	for i in range(len(list(A)) - 1):
		a = a + abs(q.index(A[i]) - q.index(A[i + 1]))
	print(a)


import sys
input = sys.stdin.readline

def print(*args, end='\n', sep=' ') -> None:
	sys.stdout.write(sep.join(map(str, args)) + end)

def solve() -> None:
	keyboard = input().strip()
	d = {}
	for (i, key) in enumerate(keyboard):
		d[key] = i
	word = input().strip()
	res = 0
	for i in range(len(word) - 1):
		res += abs(d[word[i + 1]] - d[word[i]])
	print(res)

def main() -> None:
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys
import bisect
from bisect import bisect_left as lb
from bisect import bisect_right as rb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs2(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0
a = []
(n, k) = (0, 0)
b = []
vis = []
tot = 0
time = []
time1 = []
adj = []
mx = -1
eps = 1e-07
x = 0
pref = []
lvl = []

def update_it(bit, i, val):
	n = len(bit)
	while i < n:
		bit[i] += val
		i += i & -i

def get_ans(bit, i):
	n = len(bit)
	tot = 0
	while i > 0:
		tot += bit[i]
		i -= i & -i
	return tot
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

def hnbhai(tc):
	s = sa()
	t = sa()
	d = {}
	for i in range(26):
		d[s[i]] = i
	tot = 0
	for i in range(1, len(t)):
		tot += abs(d[t[i]] - d[t[i - 1]])
	print(tot)
	return
for _ in range(sb()):
	hnbhai(_ + 1)


for _ in range(int(input())):
	(main, n) = (input(), list(map(str, input().split())))
	ls = list(*n)
	dc = {}
	for i in range(1, 27):
		dc[main[i - 1]] = i
	ans = 0
	for i in range(len(ls) - 1):
		ans += abs(dc[ls[i]] - dc[ls[i + 1]])
	print(ans)


a = int(input())
for i in range(a):
	s = input()
	w = input()
	c = 0
	for j in range(len(w) - 1):
		c += abs(s.find(w[j]) - s.find(w[j + 1]))
	print(c)


for _ in range(int(input())):
	a = input()
	a = list(a)
	s = input()
	l = []
	for i in range(len(s)):
		l.append(a.index(s[i]) + 1)
	s = 0
	for i in range(1, len(l)):
		s = s + abs(l[i] - l[i - 1])
	print(s)


from itertools import permutations, combinations, combinations_with_replacement
from bisect import bisect_left, bisect_right
from typing import List
from math import ceil, floor, gcd, sqrt
from itertools import accumulate
import string

def main():
	tt = int(input())
	for _ in range(tt):
		n = input()
		dd = {}
		for i in range(26):
			dd[n[i]] = dd.get(n[i], i + 1)
		word = input()
		s = 0
		for j in range(1, len(word)):
			s = s + abs(dd[word[j]] - dd[word[j - 1]])
		print(s)
main()


cases = int(input())
for test in range(0, cases):
	letters = str(input())
	s = str(input())
	size = len(s)
	t = []
	for i in range(0, size):
		for j in range(0, 26):
			if s[i] == letters[j]:
				t.append(j + 1)
	su = 0
	if size > 1:
		for i in range(0, size - 1):
			su += abs(t[i + 1] - t[i])
	print(su)


cases = int(input())
for _ in range(cases):
	keyboard = input()
	keyboard_dict = {}
	for (value, key) in list(enumerate(keyboard)):
		keyboard_dict[key] = value
	word = input()
	word = [char for char in word]
	ans = 0
	for i in range(len(word) - 1):
		ans += abs(keyboard_dict[word[i]] - keyboard_dict[word[i + 1]])
	print(ans)


for _ in range(int(input())):
	s = input()
	dic = {}
	for i in range(len(s)):
		dic[s[i]] = i + 1
	x = input()
	ans = 0
	for i in range(len(x) - 1):
		ans += abs(dic[x[i]] - dic[x[i + 1]])
	print(ans)


t = int(input())
for _ in range(t):
	s = input()
	b = input()
	p = 0
	i = 0
	while i < len(b) - 1:
		x = s.index(b[i])
		y = s.index(b[i + 1])
		p += abs(y - x)
		i += 1
	print(p)


t = int(input())
while t >= 1:
	t -= 1
	a = str(input())
	s = str(input())
	alpha = list(a)
	l = []
	for i in range(len(s)):
		for j in range(len(alpha)):
			if s[i] == alpha[j]:
				l.append(j + 1)
	ans = 0
	for i in range(len(l) - 1):
		ans += abs(l[i] - l[i + 1])
	print(ans)


for k in range(int(input())):
	a = list(input())
	b = input()
	s = 0
	for i in range(len(b) - 1):
		s += abs(a.index(b[i]) - a.index(b[i + 1]))
	print(s)


t = int(input())
for _ in range(t):
	letters = input()
	mat = {}
	in_ = 0
	for i in letters:
		mat[i] = in_
		in_ += 1
	word = input()
	time = 0
	for i in range(1, len(word)):
		time += abs(mat[word[i]] - mat[word[i - 1]])
	print(time)


import math

def solve(s, d):
	r = 0
	if len(s) <= 1:
		return r
	for i in range(1, len(s)):
		r += abs(d[s[i]] - d[s[i - 1]])
	return r
t = int(input())
for _ in range(t):
	s = input()
	d = {}
	for i in range(len(s)):
		d[s[i]] = i + 1
	s = input()
	print(solve(s, d))


for i in range(int(input())):
	x = input()
	y = input()
	s = 0
	for i in range(len(y) - 1):
		s += abs(x.index(y[i]) - x.index(y[i + 1]))
	print(s)


ts = int(input())
while ts > 0:
	temp = 'ygy'
	t = 0
	nums = str(input())
	sr = str(input())
	for i in range(1, len(sr)):
		temp = sr[i - 1]
		t = t + abs(nums.index(sr[i]) - nums.index(temp))
	print(t)
	ts = ts - 1


def distance(alphabet, word):
	res = 0
	last_char = word[0]
	last_index = alphabet.index(last_char)
	for char in word[1:]:
		cur_index = alphabet.index(char)
		res += abs(cur_index - last_index)
		last_index = cur_index
	return res

def std_input():
	times = input()
	for i in range(int(times)):
		alpha = input()
		word = input()
		print(distance(alpha, word))
std_input()


T = int(input())
t = 0
while t < T:
	S = str(input())
	s = str(input())
	D = 0
	G = 0
	for i in s:
		G += abs(D - S.find(i))
		D = S.find(i)
	print(G - S.find(s[0]))
	t += 1


def op():
	keyboard = list(input())
	s = list(input())
	p = keyboard.index(s[0])
	o = 0
	for i in range(len(s)):
		o += abs(keyboard.index(s[i]) - p)
		p = keyboard.index(s[i])
	return o
for i in range(int(input())):
	print(op())


from sys import stdout, stdin
from io import IOBase, BytesIO
from os import read, supports_follow_symlinks, write, fstat
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size: int=...):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))

def input():
	return stdin.readline().rstrip('\r\n')
from collections import defaultdict

def main():
	t = int(input())
	for _ in ' ' * t:
		clav = input()
		word = input()
		our_dict = defaultdict(int)
		value = 0
		for i in clav:
			our_dict[i] = value
			value += 1
		answer = 0
		current_letter = word[0]
		for i in word:
			answer += abs(our_dict[current_letter] - our_dict[i])
			current_letter = i
		print(answer)
main()


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp_vars_int = lambda : map(int, input().split())
inp_list_int = lambda : list(map(int, input().split()))
t = int(input())
for _ in range(t):
	k = {let: i for (i, let) in enumerate(input())}
	s = input()
	res = 0
	for i in range(1, len(s)):
		res += abs(k[s[i]] - k[s[i - 1]])
	print(res)


key = {}
for _ in range(int(input())):
	keyboard = input().strip()
	for i in range(26):
		key[keyboard[i]] = i
	res = 0
	s = input().strip().lower()
	for i in range(1, len(s)):
		res += abs(key[s[i]] - key[s[i - 1]])
	print(res)


import sys
from os import path
if path.exists('input.txt'):
	sys.stdout = open('output.txt', 'w')
	sys.stdin = open('input.txt', 'r')

def main():
	inp = [x for x in sys.stdin.buffer.read().split()]
	ii = 0
	out = []
	t = int(inp[ii])
	ii += 1
	for _ in range(t):
		s = str(inp[ii])[2:-1]
		ii += 1
		make = str(inp[ii])[2:-1]
		ii += 1
		ans = 0
		for i in range(len(make)):
			if i == 0:
				continue
			a = s.index(make[i])
			b = s.index(make[i - 1])
			ans += abs(a - b)
		out.append(ans)
		pass
	out_str = '\n'.join((str(x) for x in out))
	sys.stdout.write(out_str)
main()


def main():
	for _ in range(int(input())):
		kb = input()
		s = input()
		ans = 0
		for i in range(len(s) - 1):
			ans += abs(kb.index(s[i]) - kb.index(s[i + 1]))
		print(ans)
main()


for test in range(int(input())):
	keyboard = input()
	s = input()
	no_assign = {}
	for i in range(1, len(keyboard) + 1):
		no_assign[keyboard[i - 1]] = i
	ans = 0
	for i in range(1, len(s)):
		ans += abs(no_assign[s[i]] - no_assign[s[i - 1]])
	print(ans)


n = int(input())
for i in range(n):
	result = 0
	alpha = str(input())
	s = str(input())
	a = alpha.index(s[0]) + 1
	for i in range(1, len(s)):
		b = alpha.index(s[i]) + 1
		result += abs(a - b)
		a = b
	print(result)


import sys

def get_int():
	return int(input())

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def solve(s, keyboard):
	indexOf = {}
	for (i, c) in enumerate(keyboard):
		indexOf[c] = i
	result = 0
	for i in range(1, len(s)):
		result += abs(indexOf[s[i]] - indexOf[s[i - 1]])
	return result
t = get_int()
while t:
	keyboard = get_string()
	s = get_string()
	print(solve(s, keyboard))
	t -= 1


t = int(input())
for i in range(t):
	n = 26
	inp = input()
	string = list(input())
	li = []
	ans = 0
	for j in range(len(string)):
		for k in range(26):
			if string[j] == inp[k]:
				d = k + 1
				li.append(d)
	for l in range(len(li) - 1):
		ans += abs(li[l] - li[l + 1])
	print(ans)


t = int(input())
A = []
for i in range(t):
	a = input()
	b = input()
	A.append((a, b))
for i in A:
	a = i[0]
	b = i[1]
	s = 0
	if len(b) == 1:
		print(0)
	else:
		q = b[0]
		b = b[1:]
		for z in b:
			s += abs(a.index(z) - a.index(q))
			q = z
		print(s)


for _ in range(int(input())):
	s = input()
	st = input()
	res = 0
	bf = s.index(st[0])
	for i in st[1:]:
		res += abs(s.index(i) - bf)
		bf = s.index(i)
	print(res)


def distance(alpha, word):
	d = {}
	for i in range(len(alpha)):
		d[alpha[i]] = i
	ret = 0
	for i in range(1, len(word)):
		ret += abs(d[word[i]] - d[word[i - 1]])
	print(ret)
t = int(input())
for i in range(t):
	distance(input(), input())


def solve():
	m = input()
	s = input()
	mas = []
	for i in range(len(m)):
		mas.append(m[i])
	k = dict(zip(mas, list(range(1, len(mas) + 1))))
	if len(s) == 0:
		return 0
	else:
		ans = 0
		for j in range(1, len(s)):
			ans += abs(k[s[j]] - k[s[j - 1]])
		return ans
t = int(input())
for i in range(t):
	print(solve())


for _ in range(int(input())):
	s = input()
	r = input()
	d = {}
	for x in range(len(s)):
		d[s[x]] = x
	l = []
	for x in r:
		l.append(d[x])
	ans = 0
	for x in range(1, len(l)):
		ans += abs(l[x] - l[x - 1])
	print(ans)


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI1(rows_number):
	return [LI1() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline().rstrip()
inf = 4294967295
md = 998244353

def solve():
	s = SI()
	t = SI()
	ctoi = {}
	for (i, c) in enumerate(s):
		ctoi[c] = i
	ans = 0
	now = ctoi[t[0]]
	for c in t[1:]:
		i = ctoi[c]
		ans += abs(now - i)
		now = i
	print(ans)
for testcase in range(II()):
	solve()


from collections import *
for y in range(int(input())):
	s = input()
	d = defaultdict(int)
	for i in range(1, 27):
		d[s[i - 1]] = i
	s2 = input()
	ans = 0
	for i in range(1, len(s2)):
		res = abs(d[s2[i]] - d[s2[i - 1]])
		ans += res
	print(ans)


t = int(input())
while t != 0:
	a = input()
	s = input()
	net = {}
	for i in range(26):
		net[a[i]] = i + 1
	ans = 0
	for i in range(1, len(s)):
		ans += abs(net[s[i]] - net[s[i - 1]])
	print(ans)
	t -= 1


def main():
	t = int(input())
	for count in range(t):
		s = input()
		word = input()
		ans = 0
		keyboard = dict()
		for i in range(len(s)):
			keyboard[s[i]] = i
		pos_1 = keyboard[word[0]]
		for i in range(len(word)):
			ans += abs(pos_1 - keyboard[word[i]])
			pos_1 = keyboard[word[i]]
		print(ans)
main()


t = int(input())
while t:
	t -= 1
	lett = input()
	inp = input()
	(prev, ind, ans) = (-1, -1, 0)
	for i in inp:
		ind = lett.index(i)
		if prev != -1:
			ans += abs(prev - ind)
		prev = ind
	print(ans)


def solve():
	atoz = list(input())
	s = input()
	n = [atoz.index(i) + 1 for i in s]
	c = 0
	for i in range(len(n) - 1):
		c = c + abs(n[i] - n[i + 1])
	print(c)
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = list(input())
	k = list(input())
	c = 0
	for i in range(1, len(k)):
		x = n.index(k[i - 1])
		y = n.index(k[i])
		c += abs(x - y)
	print(c)


t = int(input())
for i in range(t):
	kbd = input()
	s = input()
	ans = 0
	for j in range(len(s) - 1):
		ans += abs(kbd.index(s[j]) - kbd.index(s[j + 1]))
	print(ans)


from math import *
from collections import *
import os
from io import BytesIO, IOBase
import sys
sys.setrecursionlimit(10 ** 6)
MOD = 1000000007
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binpow(a, b, m):
	a %= m
	x = 1
	while b > 0:
		if b & 1:
			x = x * a % m
		a = a * a % m
		b >>= 1
	return x

def sakurajima(n):
	d = defaultdict(list)
	if n < 9:
		n = 10
	l = [1] * n
	for i in range(2, int(n ** 0.5) + 1):
		if l[i - 1] == 1:
			for j in range(2 * i, n + 1, i):
				if j % i == 0:
					l[j - 1] = 0
					d[j].append(i)
	return d

def binser(arr, l, r, x):
	while l < r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return mid

def lcm(a, b):
	return a * b // gcd(a, b)

def sod(n):
	l = list(str(n))
	s = 0
	for i in l:
		s += int(i)
	return s

def prime_factors(num):
	l = []
	if num % 2:
		l.append(2)
	while num % 2 == 0:
		num = num / 2
	for i in range(3, int(sqrt(num)) + 1, 2):
		if not num % i:
			l.append(i)
		while num % i == 0:
			num = num / i
	if num > 2:
		l.append(num)
	return l

def factmod(n, p):
	f = defaultdict(int)
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % MOD
	return f

def largestPower(n, p):
	x = 0
	while n:
		n //= p
		x += n
	return x

def modFact(n, p):
	if n >= p:
		return 0
	res = 1
	isPrime = [1] * (n + 1)
	i = 2
	while i * i <= n:
		if isPrime[i]:
			for j in range(2 * i, n, i):
				isPrime[j] = 0
		i += 1
	for i in range(2, n):
		if isPrime[i]:
			k = largestPower(n, i)
			res = res * binpow(i, k, p) % p
	return res

def dfs(ajl, now, vis):
	vis[now] = True
	for i in ajl[now]:
		if not vis[i]:
			dfs(ajl, i, vis)

def panda(i, j):
	if i >= j:
		return l[i]
	if memo[i][j] != -1:
		return memo[i][j]
	(c1, c2) = (0, 0)
	c1 = l[i] - panda(i + 1, j)
	c2 = l[j] - panda(i, j - 1)
	memo[i][j] = max(c1, c2)
	return memo[i][j]
t1 = int(input())
for _ in range(t1):
	s1 = input()
	d = defaultdict(int)
	for i in range(26):
		if not d[s1[i]]:
			d[s1[i]] = i
	s = input()
	n = len(s)
	cnt = 0
	for i in range(1, n):
		cnt += abs(d[s[i]] - d[s[i - 1]])
	print(cnt)


output = []

def find_time(keys, s):
	time = 0
	left = 0
	right = 1
	while right < len(s):
		time += abs(int(keys.find(s[right])) - int(keys.find(s[left])))
		left += 1
		right += 1
	return time
n = int(input())
for _ in range(n):
	keys = input()
	s = input()
	output.append(find_time(keys, s))
print(*output, sep='\n')


import bisect
import math
from collections import deque
import heapq
import functools
from collections import defaultdict
mod = 998244353
N = 200005
getnums = lambda : map(int, input().split())
getnum = lambda : int(input())

def mul(a, b):
	return a * b % mod

def add(a, b):
	return a + b if a + b < mod else a + b - mod

def sub(a, b):
	return a - b if a - b >= 0 else a - b + mod

def powr(a, b):
	ans = 1
	while b > 0:
		if b & 1:
			ans = mul(ans, a)
		a = mul(a, a)
		b //= 2
	return ans

def inv(n):
	return powr(n, mod - 2)

def factlist():
	fact = [1]
	for i in range(1, N):
		fact.append(mul(fact[i - 1], i))
	return fact

def invfactlist(fact):
	invfact = [0] * N
	invfact[0] = 1
	invfact[N - 1] = inv(fact[N - 1])
	for i in range(N - 2, 0, -1):
		invfact[i] = mul(invfact[i + 1], i + 1)
	return invfact

def rot(S):
	return list(zip(*S[::-1]))

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def generate():
	ans = [0]
	while ans[-1] < 1000000000:
		ans.append(1 + ans[-1] * 2)
	return ans

def __gcd(a, b):
	if a == 0:
		return b
	return __gcd(b % a, a)

def LcmOfArray(arr, idx):
	if idx == len(arr) - 1:
		return arr[idx]
	a = arr[idx]
	b = LcmOfArray(arr, idx + 1)
	return int(a * b / __gcd(a, b))
con = dict()

class Node:

	def __init__(self, val):
		self.val = val
		self.child = None
		self.parent = None

	def addrear(self, y):
		self.child = y
		y.parent = self

	def deleterear(self, y):
		self.child = None
		y.parent = None

	def findparent(self):
		parent = self.parent
		node = self
		while parent:
			node = parent
			parent = parent.parent
		return node

	def getelem(self):
		lst = [self.val]
		ch = self.child
		while ch:
			lst.append(ch.val)
			ch = ch.child
		return lst

def main():
	t = getnum()
	while t:
		t -= 1
		s = input()
		s1 = input()
		mp = dict()
		for i in range(26):
			mp[s[i]] = i
		last = mp[s1[0]]
		ans = 0
		for i in range(1, len(s1)):
			ans += abs(last - mp[s1[i]])
			last = mp[s1[i]]
		print(ans)
main()


t = int(input())
for _ in range(t):
	keyboard = input().strip()
	text = input().strip()
	dicti = {}
	for i in range(len(keyboard)):
		dicti[keyboard[i]] = i + 1
	prev = dicti[text[0]]
	res = 0
	for i in range(1, len(text)):
		res += abs(prev - dicti[text[i]])
		prev = dicti[text[i]]
	print(res)


def II():
	return map(int, input().split())

def Ii():
	return int(input())

def infi():
	return float('Inf')

def mino():
	return -1
d = {}
for ____ in range(Ii()):
	s = input().strip()
	c = input().strip()
	ans = 0
	for i in range(len(s)):
		d[s[i]] = i
	for i in range(1, len(c)):
		ans += abs(d[c[i]] - d[c[i - 1]])
	print(ans)


from collections import Counter
for _ in range(int(input())):
	k = input()
	s = input()
	d = {}
	for each in s:
		d[each] = k.find(each)
	ans = 0
	for i in range(len(s)):
		if i > 0:
			ans += abs(d[s[i]] - d[s[i - 1]])
	print(ans)


def sol(alpha, s):
	aa = dict()
	for i in range(len(alpha)):
		aa[alpha[i]] = i + 1
	dis = 0
	for i in range(1, len(s)):
		dis += abs(aa[s[i]] - aa[s[i - 1]])
	print(dis)
t = int(input())
for test in range(t):
	alpha = input()
	s = input()
	sol(alpha, s)


n = int(input())
for _ in range(n):
	dic = {}
	time = 0
	keyboard = input()
	for i in range(len(keyboard)):
		dic[keyboard[i]] = i + 1
	word = input()
	for j in range(1, len(word)):
		time += abs(dic[word[j]] - dic[word[j - 1]])
	print(time)


for _ in range(int(input())):
	let = input()
	d = {}
	for i in range(26):
		d[let[i]] = i
	word = input()
	curr = d[word[0]]
	res = 0
	for i in range(1, len(word)):
		res += abs(curr - d[word[i]])
		curr = d[word[i]]
	print(res)


t = int(input())
for _ in range(t):
	alp = input()
	s = input()
	ans = 0
	for i in range(1, len(s)):
		ans += abs(alp.find(s[i]) - alp.find(s[i - 1]))
	print(ans)


for _ in range(int(input())):
	a = input()
	b = input()
	if len(b) == 1:
		print(0)
	else:
		s = 0
		for i in range(1, len(b)):
			s += abs(a.index(b[i - 1]) - a.index(b[i]))
		print(s)


import sys
input = sys.stdin.readline

def mp():
	return map(int, input().split())

def lmp():
	return list(map(int, input().split()))

def mps(A):
	return [tuple(map(int, input().split())) for _ in range(A)]
import math
import bisect
import heapq
from copy import deepcopy as dc
from itertools import accumulate
from collections import Counter, defaultdict, deque

def ceil(U, V):
	return (U + V - 1) // V

def modf1(N, MOD):
	return (N - 1) % MOD + 1
inf = int(1e+20)
mod = int(1000000000.0 + 7)
t = int(input())
for _ in range(t):
	s = input()[:-1]
	goal = input()[:-1]
	d = dict()
	for i in range(26):
		d[s[i]] = i
	cnt = 0
	for i in range(len(goal) - 1):
		crt = d[goal[i]]
		nxt = d[goal[i + 1]]
		cnt += abs(crt - nxt)
	print(cnt)


t = int(input())

def prob(tec, s):
	total = 0
	l = len(s)
	a = s[0]
	for b in s[1:]:
		d = abs(tec.index(a) - tec.index(b))
		total = total + d
		a = b
	print(total)
for i in range(t):
	tec = str(input())
	s = str(input())
	prob(tec, s)


for i in range(int(input())):
	cnt = 0
	st = input()
	st1 = input()
	for j in range(1, len(st1)):
		cnt += abs(st.index(st1[j]) - st.index(st1[j - 1]))
	print(cnt)


def solve():
	s = input()
	t = input()
	D = dict()
	for i in range(26):
		D[s[i]] = i
	ans = 0
	for i in range(1, len(t)):
		ans += abs(D[t[i]] - D[t[i - 1]])
	print(ans)
for _ in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	a = []
	n = input()
	a.append(n)
	s = ''.join(a)
	b = []
	m = input()
	b.append(m)
	s1 = ''.join(b)
	c = []
	for j in range(len(s1)):
		c.append(s.find(s1[j]) + 1)
	l = []
	for i in range(len(c) - 1):
		diff = abs(c[i] - c[i + 1])
		l.append(diff)
	x = sum(l)
	print(x)


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	a = input().strip()
	d = {}
	for i in range(26):
		d[a[i]] = i
	s = input().strip()
	ans = 0
	for i in range(len(s) - 1):
		ans += abs(d[s[i + 1]] - d[s[i]])
	print(ans)


def stri(s):
	return [char for char in s]

def solve():
	keyboard = stri(input())
	s = str(input())
	compt = 0
	for i in range(1, len(s)):
		compt += abs(keyboard.index(s[i - 1]) - keyboard.index(s[i]))
	print(compt)

def main():
	t = int(input())
	for i in range(t):
		solve()
main()


for i in range(int(input())):
	d = {}
	for (i, l) in enumerate(input()):
		d[l] = i
	out = 0
	s = input()
	if len(s) > 1:
		for i in range(1, len(s)):
			out += abs(d[s[i]] - d[s[i - 1]])
	print(out)


n = int(input())
for j in range(0, n):
	a = input()
	b = input()
	s = 0
	for i in range(1, len(b)):
		s += abs(a.index(b[i]) - a.index(b[i - 1]))
	print(s)


t = int(input())
for qwer in range(t):
	clav = list(input())
	sl = list(input())
	s = 0
	d = clav.index(sl[0])
	for el in sl[1:]:
		s += abs(clav.index(el) - d)
		d = clav.index(el)
	print(s)


from time import time
_START_TIME_ = time()
_DEBUGGING_ = False

def read_line() -> str:
	if _DEBUGGING_:
		return _fr_.readline()
	else:
		return input()

def write(*args, _sep=' ', _end='\n'):
	if _DEBUGGING_:
		_fw_.write(f'{_sep.join((str(elem) for elem in args))}{_end}')
	else:
		print(*args, sep=_sep, end=_end)

def log(*args, _sep=' ', _end='\n'):
	if _DEBUGGING_:
		print(*args, sep=_sep, end=_end)

def solve(kb, s):
	kb = {val: ind for (ind, val) in enumerate(kb)}
	pos = kb[s[0]]
	tim = 0
	for c in s:
		tim += abs(pos - kb[c])
		pos = kb[c]
	write(tim)
if _DEBUGGING_:
	_fr_ = open('input.txt')
	_fw_ = open('output.txt', 'w')
_t_ = int(read_line().strip())
for _ in range(_t_):
	_kb_ = read_line().strip()
	_s_ = read_line().strip()
	solve(_kb_, _s_)
if _DEBUGGING_:
	_fr_.close()
	_fw_.close()
if _DEBUGGING_:
	print('\nElapsed time: {:.2f}s'.format(time() - _START_TIME_), end='')


t = int(input())
for xx in range(t):
	s = input()
	a = input()
	c = [0 for xe in range(0, 26)]
	for i in range(len(s)):
		c[ord(s[i]) - ord('a')] = i
	pr = -1
	ans = 0
	for i in range(len(a)):
		if pr == -1:
			pr = c[ord(a[i]) - ord('a')]
		else:
			ans = ans + abs(c[ord(a[i]) - ord('a')] - pr)
			pr = c[ord(a[i]) - ord('a')]
	print(ans)


for _ in range(int(input())):
	s = input()
	w = input()
	m = {}
	for i in range(26):
		m[s[i]] = i + 1
	ans = 0
	for i in range(len(w) - 1):
		ans += abs(m[w[i]] - m[w[i + 1]])
	print(ans)


n = int(input())
for i1 in range(n):
	alf = input()
	s = input()
	d = {}
	for j in range(26):
		d[alf[j]] = j + 1
	k = 0
	for i in range(1, len(s)):
		k += abs(d[s[i]] - d[s[i - 1]])
	print(k)


N = int(input())
for _ in range(N):
	abc = input()
	d = dict()
	for (i, c) in enumerate(abc):
		d[c] = i
	s = input()
	ans = 0
	for i in range(1, len(s)):
		ans += abs(d[s[i]] - d[s[i - 1]])
	print(ans)


t = int(input())
for i in range(t):
	s = input()
	s1 = input()
	B = []
	for c1 in s1:
		for c in range(len(s)):
			if c1 == s[c]:
				B.append(c + 1)
	s2 = 0
	for j in range(len(B) - 1):
		s2 = s2 + abs(B[j] - B[j + 1])
	print(s2)


for _ in range(int(input())):
	k = input().strip()
	ks = {c: i for (i, c) in enumerate(k)}
	s = input().strip()
	ans = 0
	prev = ks[s[0]]
	for c in s[1:]:
		curr = ks[c]
		d = curr - prev
		ans += abs(d)
		prev = curr
	print(ans)


for i in range(int(input())):
	let = input()
	str = input()
	count = 0
	k = len(str)
	for i in range(1, k):
		count += abs(let.find(str[i]) - let.find(str[i - 1]))
	print(count)


for _ in range(int(input())):
	keyboard = {k: i for (i, k) in enumerate(input())}
	string = input()
	_sum = 0
	for i in range(len(string) - 1):
		_sum += abs(keyboard[string[i]] - keyboard[string[i + 1]])
	print(_sum)


n = int(input())
for i in range(n):
	s = input()
	p = str(input())
	ans = 0
	for i in range(1, len(p)):
		ans += abs(s.find(p[i]) - s.find(p[i - 1]))
	print(ans)


Test = int(input())
for T in range(Test):
	ABC = '1'
	Cad = input()
	ABC = ABC + Cad
	Palabra = input()
	Suma = 0
	PosI = ABC.index(Palabra[0])
	for K in range(1, len(Palabra)):
		PosF = ABC.index(Palabra[K])
		X = abs(PosI - PosF)
		Suma = Suma + X
		PosI = PosF
	print(Suma)


for _ in range(int(input())):
	keyboard = input()
	word = input()
	ans = 0
	prev = keyboard.index(word[0])
	for ch in word:
		for i in range(26):
			if keyboard[i] == ch:
				ans += abs(i - prev)
				prev = i
	print(ans)


def solve(l, s):
	ans = 0
	for i in range(len(s) - 1):
		ans += abs(l.index(s[i]) - l.index(s[i + 1]))
	return ans
for _ in range(int(input())):
	l = list(input())
	s = input()
	print(solve(l, s))


test = int(input())
listT = []
for k in range(test):
	board = input()
	letter = input()
	listT.append((board, letter))
for z in listT:
	boardlen = len(z[0])
	letterlen = len(z[1])
	ans = 0
	if letterlen > 1:
		location = [j for i in z[1] for j in range(0, boardlen) if z[0][j] == i]
		length = len(location)
		for l in range(0, length - 1):
			ans = ans + abs(location[l + 1] - location[l])
		print(ans)
	else:
		print(ans)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
import math
import heapq
import bisect
import collections

def ceil(a, b):
	return (a + b - 1) // b
BUFSIZE = 8192
inf = float('inf')

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
get = lambda : sys.stdin.readline().rstrip('\r\n')
MOD = 1000000007

def v():
	return list()
for _ in range(int(get())):
	s = get()
	l = get()
	ans = 0
	for i in range(len(l) - 1):
		ans += abs(s.index(l[i]) - s.index(l[i + 1]))
	print(ans)


t = int(input())
for _ in range(t):
	order = dict(zip(list(input()), range(1, 27)))
	string = input()
	time = 0
	for i in range(1, len(string)):
		time += abs(order[string[i]] - order[string[i - 1]])
	print(time)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input()

def msi():
	return map(str, input().split())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def lsi():
	return list(msi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x * y // gcd(x, y)
mod = 1000000007

def modInverse(b, m):
	g = gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

def ceil2(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def modu(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m
from math import log, factorial, cos, tan, sin, radians, floor, sqrt, ceil, log2
import bisect
import random
import string
from decimal import *
getcontext().prec = 50
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def gcd1(a):
	if len(a) == 1:
		return a[0]
	ans = a[0]
	for i in range(1, len(a)):
		ans = gcd(ans, a[i])
	return ans

def mykey(x):
	return len(x)

def main():
	for _ in range(ii()):
		o = si()
		s = si()
		ans = 0
		f = [0] * 26
		for i in range(len(o)):
			f[ord(o[i]) - 97] = i
		for i in range(1, len(s)):
			ans += abs(f[ord(s[i - 1]) - 97] - f[ord(s[i]) - 97])
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	dic = input()
	s = input()
	l = []
	for i in s:
		l.append(dic.index(i) + 1)
	res = 0
	for i in range(1, len(l)):
		res = res + abs(l[i] - l[i - 1])
	print(res)


def timeTaken(keyboard, word):
	keyMap = dict()
	for (i, letter) in enumerate(keyboard):
		keyMap[letter] = i
	totalTime = 0
	for i in range(1, len(word)):
		totalTime += abs(keyMap[word[i]] - keyMap[word[i - 1]])
	return totalTime
no_of_lines = int(input())
for i in range(no_of_lines):
	keyboard = input()
	word = input()
	print(timeTaken(keyboard, word))


n = int(input())
for i in range(n):
	kb = input()
	wd = input()
	sum = 0
	for j in range(len(wd) - 1):
		sum += abs(kb.find(wd[j]) - kb.find(wd[j + 1]))
	print(sum)


for _ in range(int(input())):
	f = input()
	s = input()
	ans = 0
	t = f.index(s[0])
	for i in range(1, len(s)):
		p = f.index(s[i])
		ans += abs(t - p)
		t = p
	print(ans)


def solve(s, ch):
	s = list(s)
	arr = []
	ans = 0
	for i in ch:
		if i in s:
			b = s.index(i) + 1
			arr.append(b)
	for i in range(len(arr) - 1):
		ans += abs(arr[i + 1] - arr[i])
	return ans
for i in range(int(input())):
	s = input()
	ch = input()
	print(solve(s, ch))


for _ in range(int(input())):
	s = input()
	inp = input()
	d = {}
	ans = 0
	for i in range(len(s)):
		d[s[i]] = i + 1
	for i in range(len(inp) - 1):
		ans += abs(d[inp[i]] - d[inp[i + 1]])
	print(ans)


t = int(input())
while t > 0:
	keyboard = input()
	s = input()
	start = -1
	total = 0
	for i in range(len(s)):
		loc = keyboard.find(s[i])
		assert loc != -1
		if start >= 0:
			total += abs(loc - start)
		start = loc
	print(total)
	t -= 1


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	temp = input().rstrip()
	s = input().rstrip()
	keyboard = dict()
	for (i, t) in enumerate(temp):
		keyboard[t] = i
	answer = 0
	for i in range(1, len(s)):
		answer += abs(keyboard[s[i]] - keyboard[s[i - 1]])
	print(answer)


from sys import stdin
from collections import defaultdict
lst = stdin.read().split()
_s = 0

def inp(n=1):
	global _s
	ret = lst[_s:_s + n]
	_s += n
	return ret

def inp1():
	return inp()[0]
t = int(inp1())
for _ in range(t):
	alph = inp1()
	d_alph = defaultdict(lambda : 0)
	for i in range(26):
		d_alph[alph[i]] = i + 1
	s = inp1()
	s_pos = []
	for l in s:
		s_pos.append(d_alph[l])
	time = 0
	for i in range(1, len(s)):
		time += abs(s_pos[i] - s_pos[i - 1])
	print(time)


test = int(input())
for i in range(test):
	n = input()
	t = input()
	res = []
	sum = 0
	for i in t:
		res.append(n.find(i))
	for i in range(0, len(res) - 1):
		sum = sum + abs(res[i + 1] - res[i])
	print(sum)


def timeTaken(board, ws):
	time = 0
	dct = {board[i]: i for i in range(26)}
	curr = ws[0]
	for c in ws[1:]:
		time += abs(dct[curr] - dct[c])
		curr = c
	return time
t = int(input())
for _ in range(t):
	keys = input()
	word = input()
	print(timeTaken(keys, word))


def collecting_input():
	n = int(input())
	cases = []
	for _ in range(n):
		(key, string) = [input(), input()]
		cases.append([key, string])
	return cases

def solution():
	cases = collecting_input()
	for i in cases:
		dictionary = {}
		time = 0
		for (j, x) in enumerate(i[0]):
			dictionary[x] = j + 1
		for l in range(1, len(i[1])):
			time += abs(dictionary[i[1][l]] - dictionary[i[1][l - 1]])
		print(time)
solution()


t = int(input())
for _ in range(t):
	k = input()
	s = input()
	res = 0
	for i in range(1, len(s)):
		res += abs(k.index(s[i]) - k.index(s[i - 1]))
	print(res)


k = int(input())
for i in range(k):
	alpha = input().strip(' ')
	dic = {}
	j = 1
	for i in alpha:
		dic[i] = j
		j += 1
	word = input().strip(' ')
	l = len(word)
	length = 0
	for i in range(l - 1):
		length += abs(dic[word[i]] - dic[word[i + 1]])
	print(length)


t = int(input())
for i in range(t):
	s = input()
	ss = input()
	ans = 0
	for x in range(1, len(ss)):
		ans += abs(s.index(ss[x - 1]) - s.index(ss[x]))
	print(ans)


import sys

def input():
	return sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	dic = dict()
	g = input()
	for idx in range(len(g)):
		dic[g[idx]] = idx
	s = input()
	sol = 0
	for idx in range(len(s) - 1):
		sol += abs(dic[s[idx]] - dic[s[idx + 1]])
	print(sol)


r = []
for _ in range(int(input())):
	kb = dict(zip(input(), range(1, 27)))
	cost = 0
	w = input()
	c0 = w[0]
	for c in w[1:]:
		cost += abs(kb[c0] - kb[c])
		c0 = c
	r.append(cost)
for cost in r:
	print(cost)


import sys

def get_int():
	return int(sys.stdin.readline())

def get_string():
	return sys.stdin.readline().strip()
FILE = False
if FILE:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = get_int()
answer = []
for i in range(0, t):
	keyboard = get_string()
	word = get_string()
	length = len(word)
	match = []
	time = 0
	for j in range(0, length):
		idx = keyboard.find(word[j])
		match.append(idx + 1)
		if j > 0:
			time = time + abs(match[j - 1] - match[j])
	answer.append(str(time))
for item in answer:
	sys.stdout.write(item)
	sys.stdout.write('\n')


t = int(input())
res = []
for i in range(t):
	d = {}
	s2 = str(input())
	for i in range(len(s2)):
		d[s2[i]] = i + 1
	s = str(input())
	ans = 0
	for i in range(1, len(s)):
		ans += abs(d[s[i]] - d[s[i - 1]])
	res.append(ans)
for i in res:
	print(i)


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
from math import *
from decimal import *
INT_MAX = 10 ** 13
from bisect import bisect_left, bisect_right
import sys
MOD = 10 ** 9 + 7
from random import randint as R
inf = 12345676543
from collections import deque
from sys import setrecursionlimit
from collections import defaultdict
for _ in range(int(input())):
	keyb = input()
	A = list(keyb)
	s = input()
	ans = 0
	for i in range(1, len(s)):
		ans += abs(A.index(s[i]) - A.index(s[i - 1]))
	print(ans)


for i in range(int(input())):
	base = input()
	dic = {base[i]: i for i in range(len(base))}
	st = input()
	ans = 0
	now = dic[st[0]]
	for i in range(1, len(st)):
		ans += abs(now - dic[st[i]])
		now = dic[st[i]]
	print(ans)


import math
t = int(input(''))
for x in range(0, t):
	al = input('')
	s = input('')
	d = {}
	if len(s) == 0:
		print(0)
	else:
		soma = 0
		for y in range(0, 26):
			d[al[y]] = y
		for i in range(1, len(s)):
			soma = soma + abs(d[s[i]] - d[s[i - 1]])
		print(soma)


t = int(input())
for _ in range(t):
	x = input()
	s = input()
	arr = [x.index(item) for item in s]
	ans = 0
	for i in range(1, len(arr)):
		ans += abs(arr[i] - arr[i - 1])
	print(ans)


def keyboard(arr, word):
	time = 0
	prev = arr.index(word[0])
	for i in word:
		index = arr.index(i)
		time += abs(prev - index)
		prev = index
	return time
t = int(input())
for _ in range(t):
	arr = list(input())
	word = input()
	print(keyboard(arr, word))


def solve(keyboard, word):
	c = 0
	dct = {}
	for symb in keyboard:
		dct[symb] = c
		c += 1
	weight = 0
	for i in range(1, len(word)):
		weight += abs(dct[word[i]] - dct[word[i - 1]])
	print(weight)
t = int(input())
for _ in range(t):
	solve(input(), input())


t = int(input())
answers = []
for i in range(t):
	keyboard = input()
	word = input()
	prev_letter = word[0]
	prev_coords = keyboard.find(prev_letter)
	word = word[1:]
	ans = 0
	for letter in word:
		if letter != prev_letter:
			coords = keyboard.find(letter)
			ans += abs(coords - prev_coords)
			prev_letter = letter
			prev_coords = coords
	answers.append(ans)
for ans in answers:
	print(ans)


def keyBoard(alp, s):
	sm = 0
	for i in range(1, len(s)):
		a = alp.index(s[i]) + 1
		b = alp.index(s[i - 1]) + 1
		sm += abs(a - b)
	return sm
t = int(input())
for i in range(t):
	alp = input()[:26]
	s = input()
	print(keyBoard(alp, s))


for _ in range(int(input())):
	A = input()
	st = input()
	d = dict()
	for i in range(26):
		d[A[i]] = i
	if len(st) == 0:
		print(0)
		continue
	if len(st) == 1:
		print(0)
		continue
	k = st[0]
	ans = 0
	for i in range(1, len(st)):
		ans += abs(d[st[i]] - d[k])
		k = st[i]
	print(ans)


for _ in range(int(input())):
	t = input()
	s = input()
	A = []
	for (i, v) in enumerate(s):
		a = t.index(v)
		A.append(a)
	res = 0
	if set(A) == 1:
		print(0)
	else:
		for i in range(1, len(A)):
			res += abs(A[i] - A[i - 1])
		print(res)


t = int(input())
while t:
	l = input()
	d = {}
	for i in range(len(l)):
		d[l[i]] = i + 1
	s = input()
	c = 0
	for i in range(1, len(s)):
		c += abs(d[s[i]] - d[s[i - 1]])
	print(c)
	t -= 1


import sys
lines = []
for Line in sys.stdin:
	lines.append(Line.rstrip('\n'))

def abs_cumulative(arr):
	return [abs(arr[i] - arr[i - 1]) for i in range(1, len(arr))]

def count_time(keys, word):
	t = []
	for c in word:
		t += [keys.index(c) + 1]
	return sum(abs_cumulative(t))
n = int(lines[0])
del lines[0]
for i in range(n):
	keys = lines[0]
	word = lines[1]
	del lines[0]
	del lines[0]
	print(count_time(keys, word))


for _ in range(int(input())):
	a = input()
	b = input()
	dp = {}
	cnt = 0
	for i in range(26):
		dp[a[i]] = i
	for i in range(len(b) - 1):
		cnt += abs(dp[b[i + 1]] - dp[b[i]])
	print(cnt)


def input_int():
	return int(input())

def input_list():
	return input().split(' ')

def input_list_int():
	return list(map(int, input_list()))
test_cases = input_int()
keyboards = []
keywords = []
while test_cases:
	keyboards.append(input())
	keywords.append(input())
	test_cases -= 1
for i in range(len(keywords)):
	dict_key = {}
	result = 0
	for letter in keywords[i]:
		if letter not in dict_key:
			dict_key[letter] = 0
	for (index, letter) in enumerate(keyboards[i]):
		if letter in dict_key:
			dict_key[letter] = index + 1
	prev = -1
	for letter in keywords[i]:
		if prev == -1:
			prev = dict_key[letter]
			continue
		result += abs(prev - dict_key[letter])
		prev = dict_key[letter]
	print(result)


n = int(input())
for i in range(n):
	s1 = input()
	s2 = input()
	m = {}
	sub = sum = 0
	for i in range(0, 26):
		m[s1[i]] = i + 1
	for j in range(len(s2) - 1):
		sum += abs(m[s2[j + 1]] - m[s2[j]])
	print(sum)


t = int(input())
for i in range(t):
	k = input()
	s = input()
	l = len(s)
	tot = 0
	for j in range(l - 1):
		p = k.find(s[j])
		q = k.find(s[j + 1])
		tot = tot + abs(q - p)
	print(tot)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_string():
	return list(map(str, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_print_int(x):
	sys.stdout.write(str(x) + '\n')

def get_print(x):
	sys.stdout.write(x + '\n')

def get_print_int_same(x):
	sys.stdout.write(str(x) + ' ')

def get_print_same(x):
	sys.stdout.write(x + ' ')
from sys import maxsize

def solve():
	for _ in range(get_int()):
		key = get_string()
		s = get_string()
		n = len(s)
		count = 0
		d = dict()
		for i in range(26):
			d[key[i]] = count
			count += 1
		ans = 0
		for i in range(n - 1):
			ans += abs(d[s[i]] - d[s[i + 1]])
		get_print_int(ans)
solve()


import sys

def solve():
	mp = {c: i for (i, c) in enumerate(input())}
	p = [mp[i] for i in input()]
	print(sum((abs(i - j) for (i, j) in zip(p, p[1:]))))
for _ in range(int(input())):
	solve()


a = int(input())
for i in range(a):
	st = str(input())
	st1 = str(input())
	ls = []
	for i in range(len(st1)):
		for j in range(len(st)):
			if st1[i] == st[j]:
				ls.append(j)
	print(sum([abs(ls[x] - ls[x - 1]) for x in range(1, len(ls))]))


def cal_str(dic_str, task_str):
	size_t = len(task_str)
	index0 = dic_str.find(task_str[0])
	index1 = 0
	totalsum = 0
	for i in range(1, size_t):
		index1 = dic_str.find(task_str[i])
		totalsum = totalsum + abs(index1 - index0)
		index0 = index1
	return totalsum
t = int(input())
dic1 = []
tasks = []
for i in range(2 * t):
	if i % 2 == 0:
		dic1.append(input())
	else:
		tasks.append(input())
for i in range(0, t):
	print(cal_str(dic1[i], tasks[i]))


t = int(input())
for i in range(t):
	s = input()
	l1 = list(s)
	s1 = input()
	l2 = list(s1)
	l3 = []
	sum = 0
	k = 0
	for i in range(0, len(l2)):
		for j in range(0, len(l1)):
			if l2[i] == l1[j]:
				l3.append(j + 1)
	l = len(l3)
	for i in range(0, l - 1):
		sum = sum + abs(l3[i + 1] - l3[i])
	print(sum)


for _ in range(int(input())):
	a = input()
	b = input()
	c = {}
	s = 0
	for i in a:
		c[i] = a.index(i)
	for i in range(len(b) - 1):
		s = s + abs(c[b[i + 1]] - c[b[i]])
	print(s)


for _ in range(int(input())):
	s = input()
	d = {x: i for (i, x) in enumerate(s)}
	k = input()
	n = len(k)
	ans = 0
	for i in range(1, n):
		ans += abs(d[k[i]] - d[k[i - 1]])
	print(ans)


t = input()
t = int(t)
n = [0] * t
m = [0] * t
for i in range(t):
	n[i] = input()
	m[i] = input()
nl = len(m)
for k in range(nl):
	n2 = len(m[k])
	sum = 0
	for z in range(n2 - 1):
		sum += abs(n[k].index(m[k][z]) - n[k].index(m[k][z + 1]))
	print(sum)


def solve(s, letter):
	alpha = {}
	c = 1
	for i in letter:
		alpha[i] = c
		c += 1
	if len(s) == 1:
		return 0
	ans = 0
	n = len(s)
	for i in range(1, n):
		ans += abs(alpha[s[i]] - alpha[s[i - 1]])
	return ans
t = int(input())
while t != 0:
	letter = input()
	s = input()
	print(solve(s, letter))
	t -= 1


for _ in range(int(input())):
	n = input()
	s = input()
	c = 0
	if len(s) == 1:
		print(0)
	else:
		for i in range(len(s) - 1):
			c += abs(n.index(s[i]) - n.index(s[i + 1]))
		print(c)


t = int(input())
for i in range(t):
	a = input()
	b = input()
	answ = 0
	pos = a.find(b[0])
	for j in b[1:]:
		answ += abs(a.find(j) - pos)
		pos = a.find(j)
	print(answ)


import sys

def rall():
	return sys.stdin.readlines()

def rl():
	return sys.stdin.readline().strip()

def rl_types(types):
	str_list = [x for x in sys.stdin.readline().strip().split(' ')]
	return [types[i](str_list[i]) for i in range(len(str_list))]

def pr(something=''):
	sys.stdout.write(str(something) + '\n')

def pra(array):
	sys.stdout.write(' '.join([str(x) for x in array]) + '\n')

def solve(array):
	return array
NT = int(rl())
for ti in range(NT):
	kb = rl()
	loc = {}
	for (i, c) in enumerate(kb):
		loc[c] = i
	s = rl()
	dist = 0
	last = loc[s[0]]
	for c in s[1:]:
		dist += abs(last - loc[c])
		last = loc[c]
	pr(dist)


x = int(input())
z = 1
t = []
c = []
while z <= x:
	kl = str(input())
	w = str(input())
	t.append(kl)
	c.append(w)
	z += 1

def sol(k_l, w):
	dic = {}
	i = 1
	for letters in k_l:
		dic[str(letters)] = i
		i += 1
	j = 0
	k = 1
	temp = 0
	while k < len(w):
		temp = temp + abs(dic[w[k]] - dic[w[j]])
		k += 1
		j += 1
	return temp
index = 0
while index < len(t):
	print(sol(t[index], c[index]))
	index += 1


t = int(input())
for j in range(t):
	letters = str(input())
	s = str(input())
	dictiony = {}
	calculating = []
	word = []
	sum = 0
	for m in s:
		word.append(m)
	for i in letters:
		dictiony[i] = letters.index(i)
	for k in word:
		calculating.append(dictiony.get(k))
	n = 0
	while n != len(word) - 1 and len(s) > 0:
		calc = abs(calculating[n + 1] - calculating[n])
		sum += calc
		n += 1
	print(sum)


t = int(input())
while t > 0:
	c = 0
	l = input()
	k = input()
	new1 = list(k)
	new = list(l)
	q = []
	for i in range(len(new1)):
		for j in range(len(new)):
			if new1[i] == new[j]:
				q.append(j + 1)
	for a in range(len(q) - 1):
		c += abs(q[a] - q[a + 1])
	t -= 1
	print(c)


T = int(input())
for t in range(T):
	s = input().strip()
	q = input().strip()
	count = 0
	if len(q) > 1:
		for i in range(len(q) - 1):
			count += abs(s.index(q[i + 1]) + 1 - (s.index(q[i]) + 1))
	print(count)


n = int(input())
ans = []
a = 0
for i in range(n):
	l = str(input())
	l = l.lower()
	q = str(input())
	q = q.lower()
	for k in range(len(q) - 1):
		s = abs(l.find(q[k + 1]) - l.find(q[k]))
		a += s
	ans.append(a)
	a = 0
for i in ans:
	print(i)


from ast import Index
for _ in range(int(input())):
	alpha = list(input())
	s = list(input())
	t = 0
	for i in range(0, len(s) - 1):
		t = t + abs(alpha.index(s[i]) - alpha.index(s[i + 1]))
	print(t)


T = int(input())

def main(keyboard, string):
	if len(string) == 0:
		return 0
	dictionary = {}
	position = 1
	for char in keyboard:
		dictionary[char] = position
		position += 1
	cost = 0
	initial_pos = dictionary[string[0]]
	for i in range(1, len(string)):
		next_pos = dictionary[string[i]]
		cost = cost + abs(next_pos - initial_pos)
		initial_pos = next_pos
	return cost
for _ in range(T):
	keyboard = input().strip()
	string = input().strip()
	print(main(keyboard, string))


t = int(input())
for o in range(t):
	sttr = input()
	test = input()
	if len(sttr) < 1:
		print('0')
	else:
		d = dict()
		for i in range(len(sttr)):
			d[sttr[i]] = i + 1
		s = 0
		for j in range(len(test) - 1):
			s += abs(d[test[j]] - d[test[j + 1]])
		print(s)


for i in range(int(input())):
	a = str(input())
	a = list(a)
	b = str(input())
	b = list(b)
	x = a.index(b[0])
	dis = 0
	for jib in b:
		fit = x
		nex = a.index(jib)
		diff = abs(nex - fit)
		dis += diff
		x = nex
	print(dis)


from sys import stdin

def _calc_score(alphabet, word):
	dct = {value: index for (index, value) in enumerate(alphabet)}
	score = 0
	for (index, value) in enumerate(word[1:], 1):
		score += abs(dct[value] - dct[word[index - 1]])
	return score
for i in range(int(stdin.readline())):
	alphabet = stdin.readline().strip()
	word = stdin.readline().strip()
	print(_calc_score(alphabet, word))


from collections import Counter, deque, defaultdict
from math import *
mod = 1000000007

def solve():
	s = input()
	d = {}
	s1 = input()
	for i in range(26):
		d[s[i]] = i
	n1 = len(s1)
	prev = d[s1[0]]
	ans = 0
	for i in range(1, n1):
		ans += abs(prev - d[s1[i]])
		prev = d[s1[i]]
	print(ans)
testcase = int(input())
for num in range(testcase):
	solve()


test_case = int(input())
for a in range(test_case):
	string = input()
	lst_of_string = list(string)
	pattern = input()
	lst_of_pattern = list(pattern)
	lst_of_index = []
	sum = 0
	for item in lst_of_pattern:
		temp = lst_of_string.index(item)
		lst_of_index.append(temp)
	for i in range(1, len(lst_of_index)):
		difference = abs(lst_of_index[i] - lst_of_index[i - 1])
		sum = sum + difference
	print(sum)


def calc(i, s):
	ans = 0
	for val in s:
		ans += abs(i - index[val])
		i = index[val]
	return ans
for _ in range(int(input())):
	keyboard = input()
	string = input()
	index = {}
	for i in range(len(keyboard)):
		index.update({keyboard[i]: i})
	a = 10 ** 9
	for i in range(len(keyboard)):
		cur_idx = index[keyboard[i]]
		a = min(a, calc(cur_idx, string))
	print(a)


t = int(input(''))
while t != 0:
	str1 = input('')
	fin = input('')
	cnt = 0
	li = []
	for ele in fin:
		p = str1.index(ele) + 1
		li.append(p)
	for i in range(0, len(li) - 1):
		diff = li[i] - li[i + 1]
		if diff < 0:
			diff = -diff
		cnt = cnt + diff
	print(cnt)
	t = t - 1