#include <bits/stdc++.h>
using namespace std;
struct ss {
  char s[20];
  bool operator<(const ss &xx) const { return strcmp(s, xx.s) < 0; }
};
int t;
int n, m;
ss a[20];
int main(void) {
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) scanf("%s", a[i].s);
    sort(a, a + n);
    if (strcmp(a[0].s, a[n - 1].s) == 0) {
      printf("%s\n", a[0].s);
      continue;
    } else {
      int chch = 0;
      for (int i = 0; i < m; i++) {
        if (a[0].s[i] != a[n - 1].s[i]) chch++;
      }
      if (chch >= 3) {
        printf("-1\n");
        continue;
      }
      if (chch == 1) {
        int w = 0;
        for (int i = 0; i < m; i++) {
          if (a[0].s[i] != a[n - 1].s[i]) {
            w = i;
            break;
          }
        }
        char b[20] = {
            0,
        };
        int go = 0;
        for (int i = 0; i < 26; i++) {
          strcpy(b, a[0].s);
          b[w] = 'a' + i;
          int ff = 0;
          for (int j = 0; j < n; j++) {
            int ch = 0;
            for (int k = 0; k < m; k++) {
              if (a[j].s[k] != b[k]) ch++;
            }
            if (ch >= 2) {
              ff = 1;
              break;
            }
          }
          if (ff == 0) {
            go = 1;
            printf("%s\n", b);
            break;
          }
        }
        if (go == 0) {
          printf("-1\n");
          continue;
        }
      } else if (chch == 2) {
        int w[10] = {
            0,
        };
        int cc = 0;
        for (int i = 0; i < m; i++) {
          if (a[0].s[i] != a[n - 1].s[i]) w[cc++] = i;
        }
        char b[20] = {
            0,
        };
        int go = 0;
        strcpy(b, a[0].s);
        b[w[0]] = a[0].s[w[0]];
        b[w[1]] = a[n - 1].s[w[1]];
        for (int i = 0; i < n; i++) {
          int cou = 0;
          for (int j = 0; j < m; j++) {
            if (b[j] != a[i].s[j]) cou++;
          }
          if (cou >= 2) {
            go = 1;
            break;
          }
        }
        if (go == 0) {
          printf("%s\n", b);
          continue;
        }
        strcpy(b, a[0].s);
        b[w[0]] = a[n - 1].s[w[0]];
        b[w[1]] = a[0].s[w[1]];
        go = 0;
        for (int i = 0; i < n; i++) {
          int cou = 0;
          for (int j = 0; j < m; j++) {
            if (b[j] != a[i].s[j]) cou++;
          }
          if (cou >= 2) {
            go = 1;
            break;
          }
        }
        if (go == 0) {
          printf("%s\n", b);
          continue;
        }
        printf("-1\n");
      }
    }
  }
  return 0;
}
