You are given two sets of integers: A and B. You need to output the sum of elements in the set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353, where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Each number should be counted only once.

For example, if A = \{2, 3\} and B = \{2, 3\} you should count integer 1 only once, despite the fact that you can get it as 3 ⊕ 2 and as 2 ⊕ 3. So the answer for this case is equal to 1 + 0 = 1.

Let's call a segment [l; r] a set of integers \\{l, l+1, ..., r\}.

The set A is given as a union of n_A segments, the set B is given as a union of n_B segments.

Input

The first line contains a single integer n_A (1 ≤ n_A ≤ 100).

The i-th of the next n_A lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}), describing a segment of values of set A.

The next line contains a single integer n_B (1 ≤ n_B ≤ 100).

The i-th of the next n_B lines contains two integers l_j and r_j (1 ≤ l_j ≤ r_j ≤ 10^{18}), describing a segment of values of set B.

Note that segments in both sets may intersect.

Output

Print one integer — the sum of all elements in set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353.

Examples

Input


2
3 5
5 8
3
1 2
1 9
2 9


Output


112


Input


1
1 9
2
2 4
2 10


Output


120

Note

In the second example, we can discover that the set C = \{0,1,...,15\}, which means that all numbers between 0 and 15 can be represented as a ⊕ b.
#include <bits/stdc++.h>
using ll = long long;
int const nmax = 100;
int const lgmax = 60;
int const modulo = 998244353;
struct Interval {
  ll start;
  int p;
  bool operator<(Interval const &oth) const {
    if (start != oth.start)
      return start < oth.start;
    else
      return oth.p < p;
  }
  bool operator==(Interval const &oth) const {
    return (start == oth.start && p == oth.p);
  }
};
std::vector<Interval> transform(std::pair<ll, ll> aux) {
  std::vector<Interval> sol;
  ll x = aux.first;
  ll y = aux.second;
  for (int h = 0; h <= lgmax; h++)
    if ((0 < (x & (1LL << h))) && (x + (1LL << h) <= y)) {
      sol.push_back({x, h});
      x += (1LL << h);
    }
  for (int h = lgmax; 0 <= h; h--)
    if ((x + (1LL << h) - 1) <= y) {
      sol.push_back({x, h});
      x += (1LL << h);
    }
  return sol;
}
Interval combine(Interval a, Interval b) {
  int pmax = std::max(a.p, b.p);
  ll result = ((a.start >> pmax) ^ (b.start >> pmax)) << pmax;
  return {result, pmax};
}
int main() {
  int n, m;
  std::cin >> n;
  std::vector<std::pair<ll, ll>> v1, v2;
  for (int i = 1; i <= n; i++) {
    ll x, y;
    std::cin >> x >> y;
    v1.push_back({x, y});
    std::vector<Interval> spart = transform({x, y});
  }
  std::cin >> m;
  for (int i = 1; i <= m; i++) {
    ll x, y;
    std::cin >> x >> y;
    v2.push_back({x, y});
    std::vector<Interval> spart = transform({x, y});
  }
  std::vector<Interval> sol;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      std::vector<Interval> s1, s2;
      s1 = transform(v1[i]);
      s2 = transform(v2[j]);
      std::vector<Interval> small;
      for (int h = 0; h < s1.size(); h++) {
        Interval base = s1[h];
        int p = base.p;
        ll bit = (1LL << p);
        ll x = (v2[j].first >> p) << p;
        ll y = (v2[j].second >> p) << p;
        sol.push_back(combine(base, {x, 0}));
        sol.push_back(combine(base, {y, 0}));
        if (x + bit <= y) {
          sol.push_back(combine(base, {x + bit, 0}));
          sol.push_back(combine(base, {y - bit, 0}));
        }
      }
      for (int h = 0; h < s2.size(); h++) {
        Interval base = s2[h];
        int p = base.p;
        ll bit = (1LL << p);
        ll x = (v1[i].first >> p) << p;
        ll y = (v1[i].second >> p) << p;
        sol.push_back(combine(base, {x, 0}));
        sol.push_back(combine(base, {y, 0}));
        if (x + bit <= y) {
          sol.push_back(combine(base, {x + bit, 0}));
          sol.push_back(combine(base, {y - bit, 0}));
        }
      }
    }
  std::sort(sol.begin(), sol.end());
  ll last = 0;
  ll result = 0;
  for (int i = 0; i < sol.size(); i++) {
    if (last <= sol[i].start) {
      ll _count = (1LL << (sol[i].p));
      result =
          (result + 1LL * sol[i].start % modulo * (_count % modulo) % modulo +
           ((_count - 1) % modulo) * ((_count / 2) % modulo)) %
          modulo;
      last = sol[i].start + (1LL << sol[i].p);
    }
  }
  std::cout << result;
  return 0;
}
