#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    string a[n];
    for (int i = 0; i < n; ++i) cin >> a[i];
    string s = a[0];
    bool checksi = true;
    for (int i = 0; i < m; ++i) {
      for (int j = 'a'; j <= 'z'; ++j) {
        s[i] = j;
        checksi = true;
        for (int k = 1; k < n; ++k) {
          int c = 0;
          for (int l = 0; l < m; ++l) {
            if (s[l] != a[k][l]) c++;
          }
          if (c > 1) {
            checksi = false;
            break;
          }
        }
        if (checksi)
          break;
        else
          s = a[0];
      }
      if (checksi) break;
    }
    if (checksi)
      cout << s << '\n';
    else
      cout << "-1\n";
  }
}
