Algorithm


A. Linear Keyboard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a keyboard that consists of 2626 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 88551212 and 1515, respectively. Therefore, it will take |58|+|125|+|1212|+|1512|=13|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 (1t10001≤�≤1000) — the number of test cases.

The next 2t2� lines contain descriptions of the test cases.

The first line of a description contains a keyboard — a string of length 2626, 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 11 to 5050 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.

Example
input
Copy
5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba
output
Copy
13
0
68
0
74

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;


#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  while(t--){
    string k, s;
    cin>>k>>s;
    int move = 0;
    for(int i = 0; i < s.length() - 1; i++){
      char curr = s[i];
      char nxt = s[i+1];
      int curr_i = k.find(curr);
      int nxt_i = k.find(nxt);
      move += abs(nxt_i - curr_i);
    }
    cout<<move<<endl;
  }

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba

Output

x
+
cmd
13
0
68
0
74
Advertisements

Demonstration


Codeforcess Solution 1607-A A. Linear Keyboard ,C++, Java, Js and Python ,1607-A,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+