#include <bits/stdc++.h>
using namespace std;
const long long INF = (1 << 30);
set<pair<int, int> > s[30];
pair<int, int> l, r;
string st, c;
int n, m;
double f(string tmp) {
  double ans = 0;
  for (int j = 0; j < tmp.size(); j++) {
    int t = tmp[j] - 97;
    s[t].insert(make_pair(j, 1));
    l = *(--s[t].find(make_pair(j, 1)));
    r = *(++s[t].find(make_pair(j, 1)));
    if (l.first == -INF && r.first == INF)
      ans = ans + int(tmp.size());
    else
      ans = ans + min(abs(double(r.first - j)), abs(double(j - l.first)));
    s[t].erase(make_pair(j, 1));
  }
  return ans;
}
int main() {
  cin >> n >> m >> st;
  for (int i = 0; i < st.size(); i++)
    s[int(st[i] - 97)].insert(make_pair(i, 0));
  for (int i = 0; i < 30; i++) {
    s[i].insert(make_pair(-INF, 0));
    s[i].insert(make_pair(INF, 0));
  }
  for (int i = 0; i < n; i++) {
    cin >> c;
    printf("%.0lf\n", f(c));
  }
  return 0;
}
