// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/intersection_of_f2_vector_spaces
#include "../../../src/io/container/lib.hpp"
#include "../../../src/lalg/basis/z2/lib.hpp"
using namespace tifa_libs;
int main() {
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
u32 t;
std::cin >> t;
while (t--) {
basis_z2<32, true> a;
u32 n, m;
std::cin >> n;
for (u32 i = 0, x; i < n; ++i) {
std::cin >> x;
a.insert(x);
}
std::cin >> m;
vec<std::bitset<32>> b;
b.reserve(m);
for (u32 i = 0, x; i < m; ++i) {
std::cin >> x;
b.emplace_back(x);
}
auto c = meet(a, b);
vecu ans;
for (auto x : c.basis)
if (x.to_ulong()) ans.push_back((u32)x.to_ulong());
std::cout << ans.size() << ' ' << ans << '\n';
}
return 0;
}
#line 1 "test/cpv/library-checker-linear_algebra/intersection_of_f2_vector_spaces.greedy.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/intersection_of_f2_vector_spaces
#line 2 "src/io/container/lib.hpp"
#line 2 "src/util/traits/others/lib.hpp"
// clang-format off
#line 2 "src/util/alias/others/lib.hpp"
#line 2 "src/util/consts/lib.hpp"
#line 2 "src/util/alias/num/lib.hpp"
#line 2 "src/util/util/lib.hpp"
// https://github.com/Tiphereth-A/CP-lib
#include <bits/extc++.h>
// clang-format off
namespace tifa_libs {
#define CEXP constexpr
#define CEXPE constexpr explicit
#define CR const&
#define CP const*
#define PC *const
#define CPC const*const
#define TPN typename
#define NE noexcept
#define CNE const noexcept
#define ND [[nodiscard]]
#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t) * 2, __VA_ARGS__, __VA_ARGS__ CR>
// NOLINTNEXTLINE(misc-const-correctness)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)
#define retif_(cond, if_true, ...) if cond return if_true __VA_OPT__(; else return __VA_ARGS__)
#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif
using namespace std::ranges;
using namespace std::literals;
template <class T>
CEXP T abs(T x) NE { retif_((x < 0), -x, x); }
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/num/lib.hpp"
// clang-format off
namespace tifa_libs {
#define mk0_(w, t) using w = t; using c##w = const t
#define mk_(w, t) mk0_(w, t); CEXP w operator""_##w(unsigned long long x) NE { return (w)x; }
mk_(i8, int8_t) mk_(u8, uint8_t) mk_(i16, int16_t) mk_(u16, uint16_t) mk_(i32, int32_t) mk_(u32, uint32_t) mk_(i64, int64_t) mk_(u64, uint64_t) mk_(isz, ssize_t) mk_(usz, size_t) mk_(chr, char) mk_(schr, signed char) mk_(uchr, unsigned char) mk_(sint, signed) mk_(uint, unsigned);
mk0_(i128, __int128_t); mk0_(u128, __uint128_t); mk0_(f32, float); mk0_(f64, double); mk0_(f128, long double);
#undef mk0_
#undef mk_
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/consts/lib.hpp"
// clang-format off
namespace tifa_libs {
using std::numbers::pi_v;
template <std::floating_point FP>
inline FP eps_v = std::sqrt(std::numeric_limits<FP>::epsilon());
template <std::floating_point FP>
CEXP void set_eps(FP v) NE { eps_v<FP> = v; }
CEXP u32 TIME = ((__TIME__[0] & 15) << 20) | ((__TIME__[1] & 15) << 16) | ((__TIME__[3] & 15) << 12) | ((__TIME__[4] & 15) << 8) | ((__TIME__[6] & 15) << 4) | (__TIME__[7] & 15);
CEXP auto STR2U16 = [] { std::array<u32, 65536> table{}; table.fill(-1_u32); flt_ (u32, i, 48, 58) flt_ (u32, j, 48, 58) table[i << 8 | j] = (j & 15) * 10 + (i & 15); return table; }();
inline const auto fn_0 = [](auto&&...) NE {};
inline const auto fn_is0 = [](auto x) NE { return x == 0; };
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/others/lib.hpp"
namespace tifa_libs {
template <class T>
struct chash {
CEXP static u64 C = u64(pi_v<f128> * 2e18) | 71;
CEXP u64 operator()(T x) CNE { return __builtin_bswap64(((u64)x ^ TIME) * C); }
};
// clang-format off
#define mk_(w, t) using w = t; using c##w = const t;
mk_(strn, std::string) mk_(strnv, std::string_view)
#undef mk_
template <class T> struct edge_t { T w; u32 u, v; CEXP auto operator<=>(edge_t CR) const = default; }; template <class T> using cedge_t = const edge_t<T>;
template <class T> struct pt3 { T _0, _1, _2; CEXP auto operator<=>(pt3 CR) const = default; }; template <class T> using cpt3 = const pt3<T>;
template <class T> struct pt4 { T _0, _1, _2, _3; CEXP auto operator<=>(pt4 CR) const = default; }; template <class T> using cpt4 = const pt4<T>;
#define mkT_(w, t, ...) template <class T> using w = t __VA_OPT__(, ) __VA_ARGS__; template <class T> using c##w = const t __VA_OPT__(, ) __VA_ARGS__;
mkT_(ptt, std::pair<T, T>) mkT_(alc, std::pmr::polymorphic_allocator<T>) mkT_(vec, std::vector<T>) mkT_(vvec, vec<vec<T>>) mkT_(v3ec, vvec<vec<T>>) mkT_(vecpt, vec<ptt<T>>) mkT_(vvecpt, vvec<ptt<T>>) mkT_(ptvec, ptt<vec<T>>) mkT_(ptvvec, ptt<vvec<T>>)
#undef mkT_
template <class T> using itl = std ::initializer_list<T>;
template <class T, usz ext = std::dynamic_extent> using spn = std::span<T const, ext>;
template <class T, usz N> using arr = std::array<T, N>; template <class T, usz N> using carr = std::array<const T, N>;
template <class U, class T> using vecp = vec<std::pair<U, T>>; template <class U, class T> using vvecp = vvec<std::pair<U, T>>;
template <class U, class T> using vvecp = vvec<std::pair<U, T>>; template <class U, class T> using vvvecp = vvec<vvec<std::pair<U, T>>>;
#ifdef PB_DS_ASSOC_CNTNR_HPP
template <class T, class C = std::less<T>> using set = __gnu_pbds::tree<T, __gnu_pbds::null_type, C>;
template <class K, class V, class C = std::less<K>> using map = __gnu_pbds::tree<K, V, C>;
// hset<u64> s({}, {}, {}, {}, {1<<16});
template <class T, class HF = chash<T>> using hset = __gnu_pbds::gp_hash_table<T, __gnu_pbds::null_type, HF>;
// hmap<u64, int> s({}, {}, {}, {}, {1<<16});
template <class K, class V, class HF = chash<K>> using hmap = __gnu_pbds::gp_hash_table<K, V, HF>;
#else
using std::set, std::map;
template <class T, class HF = chash<T>> using hset = std::unordered_set<T, HF>;
template <class K, class V, class HF = chash<K>> using hmap = std::unordered_map<K, V, HF>;
#endif
#ifdef PB_DS_PRIORITY_QUEUE_HPP
template <class T, class C = std::less<T>> using pq = __gnu_pbds::priority_queue<T, C>;
#else
template <class T, class C = std::less<T>> using pq = std::priority_queue<T, vec<T>, C>;
#endif
template <class T> using pqg = pq<T, std::greater<T>>;
// clang-format on
#define mk1_(V, A, T) using V##A = V<T>;
#define mk_(V, A, T) mk1_(V, A, T) mk1_(c##V, A, T)
#define mk(A, T) mk_(edge_t, A, T) mk_(ptt, A, T) mk_(pt3, A, T) mk_(pt4, A, T) mk_(vec, A, T) mk_(vvec, A, T) mk_(v3ec, A, T) mk_(vecpt, A, T) mk_(vvecpt, A, T) mk_(ptvec, A, T) mk_(ptvvec, A, T) mk1_(spn, A, T) mk1_(itl, A, T)
mk(b, bool) mk(c, chr) mk(i, i32) mk(u, u32) mk(ii, i64) mk(uu, u64) mk(t, isz) mk(z, usz) mk(f, f32) mk(d, f64) mk(s, strn);
#undef mk
#undef mk_
#undef mk1_
} // namespace tifa_libs
#line 4 "src/util/traits/others/lib.hpp"
namespace tifa_libs {
//! only for template without non-type argument
template <class, template <class...> class> CEXP bool specialized_from_v = false;
template <template <class...> class T, class... Args> CEXP bool specialized_from_v<T<Args...>, T> = true;
static_assert(specialized_from_v<vecu, std::vector>);
template <class T> concept container_c = common_range<T> && !std::is_array_v<std::remove_cvref_t<T>> && !std::same_as<std::remove_cvref_t<T>, strn> && !std::same_as<std::remove_cvref_t<T>, strnv>;
template <class T> concept istream_c = std::derived_from<T, std::istream> || std::derived_from<T, std::wistream> || requires(T is) { is.peek(); };
template <class T> concept ostream_c = std::derived_from<T, std::ostream> || std::derived_from<T, std::wostream> || requires(T os) { os.flush(); };
} // namespace tifa_libs
// clang-format on
#line 4 "src/io/container/lib.hpp"
namespace tifa_libs {
auto& operator>>(tifa_libs::istream_c auto& is, tifa_libs::container_c auto& x) NE {
for (auto& i : x) is >> i;
return is;
}
auto& operator<<(tifa_libs::ostream_c auto& os, tifa_libs::container_c auto CR x) NE {
if (begin(x) == end(x)) [[unlikely]]
return os;
auto it = begin(x);
for (os << *it++; it != end(x); ++it) os << ' ' << *it;
return os;
}
} // namespace tifa_libs
#line 2 "src/lalg/basis/z2/lib.hpp"
#line 2 "src/util/bitset_getdata/lib.hpp"
#line 4 "src/util/bitset_getdata/lib.hpp"
namespace tifa_libs {
template <size_t N>
struct bitset_getdata {
using word_t = std::_Base_bitset<2>::_WordT;
static CEXP size_t Nw = (N / (8 * sizeof(word_t)) + !!(N % (8 * sizeof(word_t))));
static_assert(Nw > 0);
static CEXP word_t* getdata(std::bitset<N>& x) NE {
if CEXP (Nw == 1) return &(c_(x)->_M_w);
else return c_(x)->_M_w;
}
static CEXP auto getdata(std::bitset<N> CR x) NE { return cc_(x)->_M_getdata(); }
static CEXP word_t& getword(std::bitset<N>& x, size_t pos) NE { return c_(x)->_M_getword(pos); }
static CEXP word_t getword(std::bitset<N> CR x, size_t pos) NE { return cc_(x)->_M_getword(pos); }
private:
static CEXP auto c_(std::bitset<N>& x) NE { return (std::_Base_bitset<Nw>*)(&x); }
static CEXP auto cc_(std::bitset<N> CR x) NE { return (std::_Base_bitset<Nw> const*)(&x); }
};
} // namespace tifa_libs
#line 5 "src/lalg/basis/z2/lib.hpp"
namespace tifa_libs {
template <u32 N = 64, bool greedy_insert = false>
struct basis_z2 {
vec<std::bitset<N>> basis;
CEXP basis_z2() NE : basis(N) {}
CEXP bool insert(std::bitset<N> x) NE {
bool status = false;
if CEXP (greedy_insert) { // greedy
for (u32 i = N - 1; ~i; --i)
if (x[i]) {
if (basis[i].none()) basis[i] = x;
if ((x ^= basis[i]).none()) {
status = true;
break;
}
}
} else { // GE
for (u32 i = N - 1; ~i; --i)
if (x[i]) {
if (basis[i][i]) x ^= basis[i];
else {
flt_ (u32, j, 0, i)
if (x[j]) x ^= basis[j];
flt_ (u32, j, i + 1, (u32)basis.size())
if (basis[j][i]) basis[j] ^= x;
basis[i] = x, status = true;
break;
}
}
}
return status;
}
CEXP bool test(std::bitset<N> x) CNE {
for (u32 i = N - 1; ~i; --i) {
if (!(x[i])) continue;
if (basis[i][i]) x ^= basis[i];
else return false;
}
return true;
}
ND CEXP u32 rank() CNE {
u32 res = 0;
flt_ (u32, i, 0, (u32)basis.size()) res += basis[i][i];
return res;
}
CEXP std::bitset<N> max_spn() CNE {
std::bitset<N> ret;
for (auto&& i : basis) ret ^= i;
return ret;
}
// @return std::nullopt if x is linear independent with current basis, else return the solution
CEXP auto coord(std::bitset<N> x) NE {
std::optional res{std::bitset<N>{}};
for (u32 i = basis.size() - 1; ~i; --i)
if (x[i]) {
if (!basis[i][i]) {
res = std::nullopt;
return res;
}
res->set(i), x ^= basis[i];
}
return res;
}
friend basis_z2 meet(basis_z2 CR l, vec<std::bitset<N>> CR r) NE {
if CEXP (greedy_insert) { // Zassenhaus, 2x slower with GE insertion
using bslo = bitset_getdata<N>;
using bshi = bitset_getdata<N * 2>;
using word_t = TPN bslo::word_t;
basis_z2<N * 2> tmp;
CEXP auto Nw = bslo::Nw;
for (auto CR i : l.basis) {
std::bitset<N * 2> now;
if CEXP (bshi::Nw > 1) {
memcpy(bshi::getdata(now), bslo::getdata(i), Nw * sizeof(word_t));
memcpy(bshi::getdata(now) + Nw, bslo::getdata(i), Nw * sizeof(word_t));
} else bshi::getword(now, 0) = bslo::getword(i, 0) << N | bslo::getword(i, 0);
tmp.insert(now);
}
for (auto CR i : r) {
std::bitset<N * 2> now;
if CEXP (bshi::Nw > 1) memcpy(bshi::getdata(now) + Nw, bslo::getdata(i), Nw * sizeof(word_t));
else bshi::getword(now, 0) = bslo::getword(i, 0) << N;
tmp.insert(now);
}
basis_z2 ans;
flt_ (u32, i, 0, N) memcpy(bslo::getdata(ans.basis[i]), bshi::getdata(tmp.basis[i]), Nw * sizeof(word_t));
return ans;
} else { //! will get wrong result with greedy insertion
auto cvt = [](cT_(std::bitset<N>) x) NE {
if CEXP (N <= 32) return x.to_ulong();
else if CEXP (N <= 64) return x.to_ullong();
else return x.to_string();
};
auto f = [&](std::bitset<N> x) NE {
for (auto i : l.basis)
if (auto y = x ^ i; cvt(y) < cvt(x)) x = y;
return x;
};
basis_z2<N> ans;
for (vecpt<std::bitset<N>> ab; auto x : r) {
auto y = f(x), xy = y ^ x;
for (auto [a, b] : ab)
if (auto _ = y ^ b; cvt(_) < cvt(y)) y = _, xy ^= a;
if (y.any()) ab.emplace_back(xy, y);
else ans.insert(xy);
}
return ans;
}
}
};
} // namespace tifa_libs
#line 4 "test/cpv/library-checker-linear_algebra/intersection_of_f2_vector_spaces.greedy.cpp"
using namespace tifa_libs;
int main() {
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
u32 t;
std::cin >> t;
while (t--) {
basis_z2<32, true> a;
u32 n, m;
std::cin >> n;
for (u32 i = 0, x; i < n; ++i) {
std::cin >> x;
a.insert(x);
}
std::cin >> m;
vec<std::bitset<32>> b;
b.reserve(m);
for (u32 i = 0, x; i < m; ++i) {
std::cin >> x;
b.emplace_back(x);
}
auto c = meet(a, b);
vecu ans;
for (auto x : c.basis)
if (x.to_ulong()) ans.push_back((u32)x.to_ulong());
std::cout << ans.size() << ' ' << ans << '\n';
}
return 0;
}