#include <bits/stdc++.h>
using namespace std;
constexpr long long N = 1e6 + 7;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<string> v(n);
    for (int i = 0; i < n; ++i) {
      cin >> v[i];
    }
    string s = v[0];
    bool found = true;
    for (char i = 'a'; i <= 'z'; ++i) {
      for (int j = 0; j < m; ++j) {
        found = true;
        s = v[0];
        s[j] = i;
        for (int k = 0; k < n; ++k) {
          int diffCount = 0;
          for (int l = 0; l < m; ++l) {
            if (s[l] != v[k][l]) {
              diffCount++;
            }
            if (diffCount > 1) {
              found = false;
              break;
            }
          }
          if (diffCount > 1) {
            found = false;
            break;
          }
        }
        if (found) break;
      }
      if (found) break;
    }
    if (found) {
      cout << s << endl;
    } else {
      cout << "-1" << endl;
    }
  }
  return 0;
}
