#include <iostream>
#include <vector>
#include <map>
using namespace std;
string a[2005][2005], b[410005];
string tostring(long long int a) {
if (a == 0) return "00";
string res = "";
while (a != 0) {
res = char(a % 10 + 48) + res;
a /= 10;
}
if (res.size() == 1) res = "0" + res;
return res;
}
long long int mod = 1e16;
map<long long int, pair<int, int>> mm[21];
long long int decode(string s) {
long long int x = 0, y = 0;
if (s[0] >= '0' && s[0] <= '9') x = s[0] - '0';
else x = 10 + s[0] - 'A';
if (s[1] >= '0' && s[1] <= '9') y = s[1] - '0';
else y = 10 + s[1] - 'A';
return x * 16 + y;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
string s;
cin >> s;
if (s == "message") {
int n, m;
cin >> n >> m;
vector<string> v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i += 10) {
for (int j = 1; j <= m; j++) {
v.push_back(a[i][j]);
}
}
v.push_back(tostring(n / 100));
v.push_back(tostring(n % 100));
v.push_back(tostring(m / 100));
v.push_back(tostring(m % 100));
cout << v.size() << '\n';
for (auto w : v) cout << w << ' ';
}
else {
int k;
cin >> k;
for (int i = 1; i <= k; i++) cin >> b[i];
string s = b[k - 3] + b[k - 2], t = b[k - 1] + b[k];
int n = 0, m = 0;
for (int i = 0; i < s.size(); i++) n = n * 10 + s[i] - '0';
for (int i = 0; i < t.size(); i++) m = m * 10 + t[i] - '0';
k -= 4;
for (int i = 1; i <= k; i++) {
a[(i - 1) / m + 1][(i - 1) % m + 1] = b[i];
}
for (int i = 1; i <= n / 10; i++) {
for (int j = 1; j <= m; j++) {
long long int c = 0;
for (int k = j; k < min(j + 20, m + 1); k++) {
c = (c * 257 + decode(a[i][k]) + 1) % mod;
mm[k - j + 1][c] = {i * 10 - 9, j};
}
}
}
int q;
cin >> q;
for (int jj = 0; jj < q; jj++) {
int r, x, flag = 0;
cin >> r >> x;
for (int i = 1; i <= r; i++) {
long long int c = 0;
for (int j = 1; j <= x; j++) {
string s;
cin >> s;
c = (c * 257 + decode(s) + 1) % mod;
}
if (mm[x].count(c) && flag == 0) {
pair<int, int> p = mm[x][c];
int h = p.first, k = p.second;
cout << h - i + 1 << " " << k << '\n';
if (h - i + 1 == 6 && k == 12) {
cout << r << " " << x << " " << i << '\n'
for (int l = 1; l <= 37; l++) cout << a[2][l] << " ";
cout << '\n';
for (int l = 1; l <= 37; l++) cout << a[5][l] << " ";
cout << '\n';
}
flag = 1;
}
}
}
}
}