#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

map<string, int> id;
int n, m, k;
inline int get(const string &s){
	if(id.count(s)) return id[s];
	return id[s] = k++;
}
bool e[200][200], g[200][200];
int v[200], p[200];
void dfs(int c, int col){
	v[c] = col;
	rep(i, k) if(e[c][i]){
		if(v[i] && v[i] == col) assert(0);
		if(!v[i]) dfs(i, -col);
	}
}
int root(int x){
	if(x == p[x]) return x;
	return p[x] = root(p[x]);
}

int main(){
	while(cin >> n, n){
		id.clear();
		k = 0;
		rep(i, 200) p[i] = i;
		memset(e, 0, sizeof(e));
		memset(g, 0, sizeof(g));
		memset(v, 0, sizeof(v));
		
		string s, t;
		rep(i, n){
			cin >> s;
			int pos = s.find("-");
			t = s.substr(pos + 1);
			s = s.substr(0, pos);
			int a = get(s), b = get(t);
			e[a][b] = e[b][a] = 1;
			g[a][b] = 1;
			a = root(a); b = root(b);
			if(a != b) p[a] = b;
		}
		rep(i, k) if(!v[i]) dfs(i, 1);
		rep(i, k) rep(j, k) e[i][j] = g[i][j];
		rep(i, k) rep(j, k){
			bool ok = 0, ng = 0;
			rep(l, k){
				if(e[l][i] && e[j][l] || e[l][j] && e[i][l]) ng = 1;
				if(e[l][i] && e[l][j] || e[i][l] && e[j][l]) ok = 1;
			}
			if(ok && !ng) g[i][j] = g[j][i] = 1;
		}
		rep(l, k) rep(i, k) rep(j, k) g[i][j] |= g[i][l] && g[l][j];
		
		cout << k << endl;
		cin >> m;
		rep(i, m){
			cin >> s;
			int p = s.find("-");
			t = s.substr(p + 1);
			s = s.substr(0, p);
			int a = get(s), b = get(t);
			
			if(root(a) != root(b) || v[a] == v[b] || !g[a][b])
				cout << "NO" << endl;
			else cout << "YES" << endl;
		}
	}
	return 0;
}