#include <bits/stdc++.h>
using namespace std;
vector<int> id[256];
int n, k;
string s;
int main() {
  cin >> n >> k >> s;
  for (int i = 0; i < k; i++) {
    id[s[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> s;
    long long f = 0;
    for (int j = 0; j < s.size(); j++) {
      int Min = 1e9;
      for (int k : id[s[j]]) {
        Min = min(Min, abs(k - j));
      }
      f += Min == 1e9 ? s.size() : Min;
    }
    cout << f << endl;
  }
}
