#include<bits/stdc++.h>

using namespace std;

#define FOR(i, mx) for(int i = 0; i < mx; i++)
#define FORI(i, mx, ini) for(int i = ini; i < mx; i++)

void solve(int E, int M, int P){
    unordered_map<int,unordered_map<int,bool>> qr;
    unordered_map<int,unordered_map<int,bool>> n_qr;
    int qnt = 0;
    FOR(i,M){
        int i1, i2;
        cin >> i1 >> i2;
        qr[min(i1,i2)][max(i1,i2)] = true;
        qnt++;
    }

    FOR(i, P){
        int i1, i2;
        cin >> i1 >> i2;
        n_qr[min(i1,i2)][max(i2,i1)] = true; 
    }

    FOR(i,E/3){
        set<int> st;
        FOR(j, 3){
            int n;
            cin >> n;
            st.insert(n);
        }
        FOR(k, 3) FORI(j, 3, k){
            auto f = st.begin();
            auto g = st.begin();
            advance(f, k);
            advance(g, j);
            if(qr[*f][*g]) qnt--;
            else if(n_qr[*f][*g]) qnt++;
        }
    }

    cout << qnt << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int E,M,P;
    while(true){
        cin >> E;
        if(E == 0) break;
        cin >> M >> P;
        solve(E,M,P);
    }
}