#include <iostream>
using namespace std;

//int find(int* arr, int s) {
  //int r;
  //for (r = s; r != arr[r]; r = arr[r])
    //arr[r] = arr[arr[r]];
//}
  int id[1000000], sz[1000000];

int main()
{
  int N; cin >> N;
  for (int I = 1; I <= N; ++I) {
    int n, k; cin >> n >> k;
    for (int i = 0; i < n; ++i) {
      id[i] = i; sz[i] = 1;
    }
    for (int i = 0; i < k; ++i) {
      int a, b; cin >> a >> b;
      //cout << a << ' ' << b << endl;
      int ix, jx;
      for (ix = a; ix != id[ix]; ix = id[ix])
        id[ix] = id[id[ix]];
      for (jx = b; jx != id[jx]; jx = id[jx])
        id[jx] = id[id[jx]];
      if (ix == jx) continue;
      if (sz[ix] < sz[jx]) {
        id[ix] = jx; sz[jx] += sz[ix];
      } else {
        id[jx] = ix; sz[ix] += sz[jx];
      }
    }

    cout << "Scenario " << I << ":" << endl;
    int m; cin >> m;
    for (int i = 0; i < m; ++i) {
      int a, b; cin >> a >> b;
      int ix, jx;
      for (ix = a; ix != id[ix]; ix = id[ix])
        id[ix] = id[id[ix]];
      for (jx = b; jx != id[jx]; jx = id[jx])
        id[jx] = id[id[jx]];
      cout << (ix == jx) << endl;
    }
    cout << endl;
  }
}

