<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

406. Goggle
Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



Everybody knows search engine Goggle. But only advanced users know that it is possible to search number sequences. You can enter a set of numbers and the search engine will find all sequences which contain it. Goggle developers decided to improve the engine. New feature will help you if you know numbers which shouldn't be in the result. These numbers must be entered with the opposite sign. For example, if somebody enters "5 -3 6", the engine will find all the sequences which contain 5 and 6, but do not contain 3.

Help Goggle developers to implement the feature.

Input
The first line of the input will contain two integer numbers n and m (1 ≤ n ≤ 10, 1 ≤ m ≤ 10), where n is the number of sequences in Goggle database and m is the number of queries. Following n lines describe sequences in the Goggle database. The first integer k in each line is the length of the sequence (1 ≤ k ≤ 10). Next k numbers are the sequence elements. All of them are integers between 1 and 100, inclusive. Following m lines describe queries. The first integer l of each line is the numbers in query (1 ≤ l ≤ 10). Next l numbers bi are the sequence elements (1 ≤ |bi| ≤ 100, bi ≠ 0). These numbers have different absolute values.

Output
For each query print t — the number of found sequences on a separate line. Each of the next t lines should contain found sequence. The relative order of sequences should be preserved (in compliance with the input). The order of numbers in sequences should not change (in compliance with the input). Write sequences in format as they were given in the input.

Example(s)
sample input
sample output
3 5
6 1 2 3 1 2 3
4 3 2 4 5
2 4 2
3 1 2 3
2 3 2
3 2 -1 3
2 4 -2
2 4 5
1
6 1 2 3 1 2 3
2
6 1 2 3 1 2 3
4 3 2 4 5
1
4 3 2 4 5
0
1
4 3 2 4 5

<|response|>
1. Abridged Problem Statement
You have n stored integer sequences (1 ≤ n ≤ 10), each of length up to 10, with elements in [1,100]. Then you receive m queries (1 ≤ m ≤ 10). Each query consists of a list of signed integers b₁,…,bₗ (1 ≤ l ≤ 10). A positive bᵢ means "this number must appear" and a negative bᵢ means "its absolute value must not appear." For each query, output how many of the stored sequences satisfy all these requirements, and then list those sequences in their original format and input order.

2. Key Observations
- Constraints are very small (n, m, k, l ≤ 10), so a simple brute-force check for each query against each sequence is more than fast enough.
- To test membership quickly, store for each sequence both:
  • The original list of its elements (for output).
  • A set of its values for fast lookup.
- For each query and for each sequence, iterate through the query terms:
  • If bᵢ > 0 and bᵢ is not in the sequence, reject.
  • If bᵢ < 0 and |bᵢ| is in the sequence, reject.
  Otherwise accept.

3. Full Solution Approach
- Read n and m.
- For i from 1 to n:
  • Read kᵢ and then the kᵢ elements of sequence i.
  • Store them in a vector seqs[i].
  • Also record a set present[i] of its values for fast lookup.
- For each of the m queries:
  • Read l and then the l signed integers b₁…bₗ.
  • Initialize an empty list of matches.
  • For each sequence i=1…n:
    – Assume ok = true.
    – For each b in the query list:
      · If b > 0 and b ∉ present[i], set ok = false.
      · If b < 0 and (−b) ∈ present[i], set ok = false.
    – If ok remains true, append i to matches.
  • Print the number of matches. Then for each matched index i, print the original sequence in the form "kᵢ x₁ x₂ … xₖᵢ."

Time complexity per query: O(n·l), which is at most 100 operations here—trivial.

4. C++ Implementation
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, m;
vector<vector<int>> seqs;
vector<set<int>> present;

void read() {
    cin >> n >> m;
    seqs.assign(n, {});
    present.assign(n, {});
    for(int i = 0; i < n; i++) {
        int k;
        cin >> k;
        seqs[i].resize(k);
        cin >> seqs[i];
        for(int x: seqs[i]) {
            present[i].insert(x);
        }
    }
}

void solve() {
    // Each database sequence stores its element multiset (as a vector to
    // preserve order) and a set of present values for O(log) membership tests.
    // A query lists wanted values (positive) and unwanted values (negative);
    // a sequence matches if it contains every positive value and none of the
    // values whose negation is given. We scan sequences in input order and
    // print the matching ones in their original format.

    while(m--) {
        int l;
        cin >> l;
        vector<int> query(l);
        cin >> query;
        vector<int> matched;
        for(int i = 0; i < n; i++) {
            bool ok = true;
            for(int q: query) {
                if(q > 0 && !present[i].count(q)) {
                    ok = false;
                } else if(q < 0 && present[i].count(-q)) {
                    ok = false;
                }
            }

            if(ok) {
                matched.push_back(i);
            }
        }

        cout << matched.size() << '\n';
        for(int idx: matched) {
            cout << seqs[idx].size() << ' ' << seqs[idx] << '\n';
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    read();
    solve();

    return 0;
}
```

5. Python Implementation with Detailed Comments
```python
import sys

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    n, m = int(next(it)), int(next(it))

    # Read and store each sequence and its element‐set
    seq = []
    has = []
    for _ in range(n):
        k = int(next(it))
        s = []
        sset = set()
        for __ in range(k):
            x = int(next(it))
            s.append(x)
            sset.add(x)
        seq.append(s)
        has.append(sset)

    out_lines = []
    # Process queries
    for _ in range(m):
        l = int(next(it))
        query = [int(next(it)) for __ in range(l)]

        matches = []
        # Check each sequence
        for i in range(n):
            valid = True
            sset = has[i]
            for b in query:
                if b > 0:
                    # required number missing?
                    if b not in sset:
                        valid = False
                        break
                else:
                    # forbidden number present?
                    if -b in sset:
                        valid = False
                        break
            if valid:
                matches.append(i)

        # Record output
        out_lines.append(str(len(matches)))
        for idx in matches:
            s = seq[idx]
            out_lines.append(str(len(s)) + " " + " ".join(map(str, s)))

    sys.stdout.write("\n".join(out_lines))

if __name__ == "__main__":
    main()
```
