#include<iostream>
#include<cassert>
using namespace std;

int H,W;
int n,p;
int h[10],w[10];
string s[30];
int k,id[10];

int main(){
  while(cin >> H >> W){
    assert(H || W);
    cerr << H << " " << W << endl;
    for(int i=0;i<H;i++){
      cin >> s[i];
      //cout << s[i] << endl;
    }

    cin >> n;
    cerr << n << endl;
    for(int i=0;i<n;i++){
      cin >> h[i] >> w[i];
      //cerr << h[i] << " " << w[i] << endl;
      int num = 0;
      for(int j=0;j<h[i];j++){
	cin >> s[j];
	//cerr << s[j] << endl;
      }
    }

    cin >> p;
    cerr << p << endl;
    for(int i=0;i<p;i++){
      cin >> k;
      //cerr << k << " ";
      for(int j=0;j<k;j++){
	cin >> id[i];
	//cerr << id[i] << " ";
      }
    }
  }
}