#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
vector<vector<int> > letters(26);
vector<string> tries;
vector<long long> ans;
int n, k;
int bins(vector<int> wh, int x) {
  size_t first = 0, last = wh.size(), mid;
  if (wh.size() == 0) return -1;
  while (first < last) {
    mid = first + (last - first) / 2;
    if (x <= wh[mid])
      last = mid;
    else
      first = mid + 1;
  }
  if (last == 0 ||
      last < wh.size() && abs(wh[last] - x) < abs(wh[last - 1] - x))
    return abs(wh[last] - x);
  else
    return abs(wh[last - 1] - x);
}
int main() {
  cin >> n >> k;
  tries.resize(n);
  ans.resize(n);
  for (int i = 0; i < (int)(k); i++) {
    char x;
    cin >> x;
    letters[x - 'a'].push_back(i);
  }
  for (int i = 0; i < (int)(n); i++) cin >> tries[i];
  for (int i = 0; i < (int)(n); i++) {
    for (int j = 0; j < (int)(tries[i].length()); j++) {
      int k = bins(letters[tries[i][j] - 'a'], j);
      if (k == -1)
        ans[i] += tries[i].length();
      else
        ans[i] += k;
    }
  }
  for (int i = 0; i < (int)(n); i++) cout << ans[i] << endl;
  return 0;
}
