#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
  int N; cin >> N;
  for (int I = 1; I <= N; ++I) {

    int m; cin >> m; cin.get();
    vector<string> pool(m);
    for (int i = 0; i < m; ++i)
      getline(cin, pool[i]);

    cout << "Scenario #" << I << ":" << endl;

    int n; cin >> n;
    for (int i = 0; i < n; ++i) {
      int k; cin >> k;
      for (int j = 0; j < k; ++j) {
        int index; cin >> index;
        cout << pool[index];
      }
      cout << endl;
    }
    cout << endl;
  }
}

