#include <bitset>
#include <cstdint>
#include <iostream>

using ll = int64_t;

constexpr ll maxn = 3e4;
ll n,m,q;
std::bitset<maxn+5> bs[maxn+5];
std::bitset<maxn+5> none;

int main(){
    std::cin>>n>>m>>q;
    for(ll i=1;i<=m;i++){
        none[i]=true;
    }
    for(ll i=1;i<=n;i++){
        static ll num;
        std::cin>>num;
        for(ll j=1;j<=num;j++){
            static ll tmp;
            std::cin>>tmp;
            bs[i][tmp]=true;
        }
    }
    for(ll i=1;i<=q;i++){
        static ll f,x,y;
        std::cin>>f>>x>>y;
        if(f==1){
            bs[x]<<=y;
            bs[x]&=none;
        }else if(f==2){
            bs[x]>>=y;
            bs[x]&=none;
        }else if(f==3){
            std::cout<<(bs[x]&bs[y]).count()<<'\n';
        }else if(f==4){
            std::cout<<(bs[x]|bs[y]).count()<<'\n';
        }else if(f==5){
            std::cout<<(bs[x]^bs[y]).count()<<'\n';
        }
    }
}