import java.io.*; /** * Created by pdhinwa on 21/04/17. */ public class Main { void main() throws IOException { Reader reader = new Reader(); Writer writer = new Writer(); int T = reader.readInt(); Checker.check(T, 1, 10); while (T-- > 0) { int[] a = reader.readArray(); Checker.check(a.length == 2); int N = a[0]; int K = a[1]; Checker.check(N, 1, 100000); Checker.check(K, 1, (int) 1e9); Checker.check(N * (long) K, 1, (long) 1e9); String s = reader.readString(); Checker.check(s.length() == N); for (int i = 0; i < N; i++) { Checker.check(s.charAt(i), 'a', 'z'); } long res = 0; int cnta = 0; int cntb = 0; for (int i = 0; i < N; i++) { if (s.charAt(i) == 'a') { cnta++; } if (s.charAt(i) == 'b') { res += cnta; cntb++; } } // (a, b) lie in the same group of strings. long ans = res * K; // (a, b) belong to different group of strings. ans += K * (long) (K - 1) / 2 * cnta * cntb; writer.println(ans); } writer.close(); reader.readEof(); } public static void main(String[] args) throws IOException { try { new Main().main(); } catch (Exception e) { e.printStackTrace(); throw e; } } private class Writer extends PrintWriter { Writer() throws FileNotFoundException { super(System.out); } Writer(File file) throws FileNotFoundException { super(file); } } private class Reader { String[] data; BufferedReader bufferedReader; Reader() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } Reader(File file) throws FileNotFoundException { bufferedReader = new BufferedReader(new FileReader(file)); } String readString() throws IOException { return bufferedReader.readLine(); } void readEof() throws IOException { Checker.check(readString() == null); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readArray() throws IOException { data = readString().split(" "); int[] res = new int[data.length]; for (int i = 0; i < res.length; i++) { res[i] = Integer.parseInt(data[i]); } return res; } } private static class Checker { private static void check(long x, long L, long R) { if (x < L || x > R) { throw new RuntimeException(); } } private static void check(boolean b) { if (!b) { throw new RuntimeException(); } } private static void check(int x, int L, int R) { if (x < L || x > R) { throw new RuntimeException(); } } } }