#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
vector<vector<string> > dp;
int n, m;
string resp = "";
vector<string> a;
vector<set<char> > occr;
string solve(int at, int estado) {
  if (at == m) {
    return "";
  }
  if (dp[at][estado] != "") {
    return dp[at][estado];
  }
  for (char c : occr[at]) {
    bool pode = true;
    for (int I = 0; I < n; I++) {
      if (a[I][at] != c && (estado & (1 << I))) {
        pode = false;
      }
    }
    if (!pode) {
      continue;
    }
    int temp = estado;
    for (int I = 0; I < n; I++) {
      if (a[I][at] != c) {
        temp |= (1 << I);
      }
    }
    string res = solve(at + 1, temp);
    if (res != " ") {
      return dp[at][estado] = c + res;
    }
  }
  return dp[at][estado] = " ";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    a.clear();
    a.resize(n);
    occr.clear();
    occr.resize(m);
    dp.clear();
    dp.resize(11, vector<string>(1030, ""));
    for (int I = 0; I < n; I++) {
      cin >> a[I];
    }
    for (int I = 0; I < n; I++) {
      for (int J = 0; J < m; J++) {
        occr[J].insert(a[I][J]);
      }
    }
    string resp = solve(0, 0);
    if (resp == " ") {
      cout << -1 << '\n';
    } else {
      cout << resp << '\n';
    }
  }
  return 0;
}
