#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif
#include "string/RollingHash2D.hpp"
using namespace std;
typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
template <class T> istream& operator>>(istream& is, vector<T>& v) {
for (auto& x : v) is >> x;
return is;
}
template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
auto sep = "";
for (const auto& x : v) os << exchange(sep, " ") << x;
return os;
}
template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }
template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }
template <class T> void mkuni(vector<T>& v) {
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
}
template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }
const int MAX = 16, ROW = 3, COL = 4;
int ctoi(char c) { return isdigit(c) ? c - '0' : 10 + c - 'A'; }
char itoc(int n) { return n < 10 ? '0' + n : 'A' + (n - 10); }
int htoi(const string& s) { return ctoi(s[0]) * MAX + ctoi(s[1]); }
string itoh(int n) {
string res = "";
return res + itoc(n / MAX) + itoc(n % MAX);
}
void solve1() {
int r, c;
cin >> r >> c;
vector S(r, vector<string>(c));
cin >> S;
vector<string> ans;
ans.emplace_back(itoh(r / (MAX * MAX)));
ans.emplace_back(itoh(r % (MAX * MAX)));
ans.emplace_back(itoh(c / (MAX * MAX)));
ans.emplace_back(itoh(c % (MAX * MAX)));
for (int i = 0; i < r; i += ROW) {
for (int j = 0; j < c; j += COL) {
ans.emplace_back(S[i][j]);
}
}
cout << ans.size() << '\n';
cout << ans << '\n';
}
void solve2() {
int m;
cin >> m;
vector<string> S(m);
cin >> S;
int r = htoi(S[0]) * (MAX * MAX) + htoi(S[1]);
int c = htoi(S[2]) * (MAX * MAX) + htoi(S[3]);
vector<vector<int>> mass;
for (int i = 0, cur = 4; i < r; i += ROW) {
vector<int> tmp;
for (int j = 0; j < c; j += COL) {
tmp.emplace_back(htoi(S[cur++]));
}
mass.emplace_back(tmp);
}
RollingHash2D RH;
auto hashes = RH.build(mass);
map<pair<int, int>, map<uint64_t, pair<int, int>>> mp;
auto calc = [&](int x, int y) {
map<uint64_t, pair<int, int>> res;
for (int i = 0; i + x <= int(mass.size()); i++) {
for (int j = 0; j + y <= int(mass[0].size()); j++) {
res[RH.query(hashes, i, i + x, j, j + y).val()] = {i * ROW, j * COL};
}
}
mp[{x, y}] = res;
};
auto check = [&](int x, int y, uint64_t val) -> pair<int, int> {
if (not mp.count({x, y})) calc(x, y);
return mp[{x, y}].count(val) ? mp[{x, y}][val] : make_pair(-1, -1);
};
int q;
cin >> q;
for (; q--;) {
int h, w;
cin >> h >> w;
vector target(h, vector<int>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
string tmp;
cin >> tmp;
target[i][j] = htoi(tmp);
}
}
int ansx = -1, ansy = -1;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
int cr = 0, cc = 0;
for (int k = i; k < h; k += ROW) cr++;
for (int k = j; k < w; k += COL) cc++;
vector<vector<int>> base;
for (int k = i; k < h; k += ROW) {
vector<int> tmp;
for (int l = j; l < w; l += COL) {
tmp.emplace_back(target[k][l]);
}
base.emplace_back(tmp);
}
auto res = check(cr, cc, RH.get(base).val());
if (res.first != -1) ansx = res.first - i, ansy = res.second - j;
}
}
cout << ansx + 1 << ' ' << ansy + 1 << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string op;
cin >> op;
if (op == "message")
solve1();
else
solve2();
return 0;
}