#include <bits/stdc++.h>
using namespace std;
int cond = 1;
set<int> pos[255];
set<int> pos2[255];
char s[300005];
int n, k;
int main() {
  ios_base::sync_with_stdio(false);
  scanf("%d %d", &n, &k);
  scanf("%s", s);
  for (int i = 0; i < k; ++i) {
    pos[s[i]].insert(i);
    pos2[s[i]].insert(-i);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    int len = strlen(s);
    long long res = 0;
    for (int j = 0; j < len; ++j) {
      int left = -1, right = -1;
      if (pos[s[j]].lower_bound(j) != pos[s[j]].end())
        right = *pos[s[j]].lower_bound(j);
      if (pos2[s[j]].lower_bound(-j) != pos2[s[j]].end())
        left = -*pos2[s[j]].lower_bound(-j);
      if (left == -1 && right == -1)
        res += len;
      else if (left != -1 && right != -1)
        res += min(j - left, right - j);
      else if (left != -1)
        res += j - left;
      else
        res += right - j;
    }
    cout << res << "\n";
  }
  return 0;
}
