#include <bits/stdc++.h>
using namespace std;
inline long long int modadd(long long int n, long long int m,
                            long long int p = 1000000007) {
  return ((n + m) % p + p) % p;
}
inline long long int modsub(long long int n, long long int m,
                            long long int p = 1000000007) {
  return ((n - m + p) % p + p) % p;
}
inline long long int modpro(long long int n, long long int m,
                            long long int p = 1000000007) {
  return (((n % p) * (m % p)) % p + p) % p;
}
unsigned long long int powe(long long int first, long long int second) {
  unsigned long long int res = 1;
  while (second > 0) {
    if (second & 1) res = res * first;
    second = second >> 1;
    first = first * first;
  }
  return res;
}
long long int modpow(long long int first, long long int second,
                     long long int p = 1000000007) {
  long long int res = 1;
  while (second > 0) {
    if (second & 1) res = modpro(res, first, p);
    second = second >> 1;
    first = modpro(first, first, p);
  }
  return res;
}
inline long long int modInverse(long long int n, long long int p = 1000000007) {
  if (n == 1) return 1;
  return modpow(n, p - 2, p);
}
inline long long int moddiv(long long int n, long long int m,
                            long long int p = 1000000007) {
  return modpro(n, modInverse(m, p), p);
}
inline long long int modadd3(long long int first, long long int second,
                             long long int z, long long int p = 1000000007) {
  return modadd(modadd(first, second, p), z, p);
}
inline long long int modadd4(long long int first, long long int second,
                             long long int z, long long int w,
                             long long int p = 1000000007) {
  return modadd(modadd(first, second, p), modadd(z, w, p), p);
}
inline long long int modnCr(long long int fac[], int n, int r,
                            long long int p = 1000000007) {
  if (r == 0) return 1;
  return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p);
}
template <typename T>
inline T max3(T first, T second, T z) {
  return max(max(first, second), z);
}
template <typename T>
inline T max4(T first, T second, T z, T w) {
  return max(max3(first, second, w), z);
}
template <typename T>
inline T min3(T first, T second, T z) {
  return min(min(first, second), z);
}
template <typename T>
inline T min4(T first, T second, T z, T w) {
  return min(min3(first, second, w), z);
}
template <typename T>
void printArr(T *arr, int s, int n) {
  for (int i = s; i <= n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
}
vector<pair<long long int, int>> v;
void primeFactors(long long int n) {
  v.clear();
  int c = 0;
  while (n % 2 == 0) {
    n = n / 2;
    c++;
  }
  if (c > 0) v.push_back({2, c});
  for (long long int i = 3; i * i <= n; i = i + 2) {
    c = 0;
    while (n % i == 0) {
      n = n / i;
      c++;
    }
    if (c > 0) v.push_back({i, c});
  }
  if (n > 2) v.push_back({n, 1});
}
string s[13];
int m, n;
vector<string> ans;
bool check(string &s1) {
  for (int i = (0); i < (n); i++) {
    int c = 0;
    for (int k = (0); k < (m); k++) {
      if (s1[k] != s[i][k]) {
        c++;
      }
    }
    if (c > 1) return 0;
  }
  return 1;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int erer = 1;
  cin >> erer;
  for (int erer2 = (1); erer2 < (erer + 1); erer2++) {
    cin >> n;
    cin >> m;
    for (int i = (0); i < (n); i++) cin >> s[i];
    ans.clear();
    int f = 1;
    for (int i = (0); i < (n); i++) {
      for (int j = (i + 1); j < (n); j++) {
        int c = 0;
        for (int k = (0); k < (m); k++) {
          if (s[i][k] != s[j][k]) {
            c++;
          }
        }
        if (c > 2) {
          f = -1;
          break;
        }
        if (c == 2) {
          if (((long long int)(ans).size())) continue;
          string st(s[i]);
          for (int k = (0); k < (m); k++)
            if (s[i][k] != s[j][k]) {
              st[k] = s[j][k];
              break;
            }
          ans.push_back(st);
          string st2(s[j]);
          for (int k = (0); k < (m); k++)
            if (s[i][k] != s[j][k]) {
              st2[k] = s[i][k];
              break;
            }
          ans.push_back(st2);
        }
      }
      if (f == -1) break;
    }
    if (f == -1)
      cout << -1 << endl;
    else if (((long long int)(ans).size()) == 0) {
      cout << s[0] << endl;
    } else {
      if (check(ans[0]))
        cout << ans[0] << endl;
      else if (check(ans[1]))
        cout << ans[1] << endl;
      else
        cout << -1 << endl;
    }
  }
  return 0;
}
