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 namespace std;
mt19937 rng(static_cast<unsigned int>(
    chrono::steady_clock::now().time_since_epoch().count()));
template <typename num_t>
inline void add_mod(num_t& a, const long long& b, const num_t& m) {
  a = (a + b) % m;
  if (a < 0) a += m;
}
template <typename num_t>
inline bool update_max(num_t& a, const num_t& b) {
  return a < b ? a = b, true : false;
}
template <typename num_t>
inline bool update_min(num_t& a, const num_t& b) {
  return a > b ? a = b, true : false;
}
template <typename num_t>
num_t gcd(const num_t& lhs, const num_t& rhs) {
  return !lhs ? rhs : gcd(rhs % lhs, lhs);
}
template <typename num_t>
num_t lcm(const num_t& lhs, const num_t& rhs) {
  return lhs / ::gcd(lhs, rhs) * rhs;
}
template <typename num_t>
num_t pw(num_t n, num_t k, const num_t& mod) {
  if (k == -1) k = mod - 2;
  num_t res = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1) res = 1ll * res * n % mod;
    n = 1ll * n * n % mod;
  }
  return res % mod;
}
const int inf = 1e9 + 7;
const int mod = 998244353;
const long long ll_inf = (long long)inf * inf * 9 + 7;
const int maxn = 1000 + 7;
const int base = 61;
vector<pair<long long, int>> get_nodes_from_range(long long lhs,
                                                  long long rhs) {
  vector<pair<long long, int>> ans;
  while (lhs <= rhs) {
    int j = 0;
    for (j = 0; j < base; ++j) {
      long long range = 1ll << j;
      long long nxt = lhs + range - 1;
      if (nxt <= rhs && (lhs >> j) == (nxt >> j)) {
      } else
        break;
    }
    ans.push_back({lhs, j - 1});
    --j;
    lhs += 1ll << j;
  }
  return ans;
}
vector<vector<pair<long long, int>>> get_nodes_from_input() {
  int n;
  cin >> n;
  vector<vector<pair<long long, int>>> res;
  for (int i = (0), _b = (n); i < _b; ++i) {
    long long lhs, rhs;
    cin >> lhs >> rhs;
    vector<pair<long long, int>> temp = get_nodes_from_range(lhs, rhs);
    res.push_back(temp);
  }
  return res;
}
pair<long long, int> get_transform_node(pair<long long, int> lhs,
                                        pair<long long, int> rhs) {
  int bit_count = max(lhs.second, rhs.second);
  long long first_value = ((lhs.first ^ rhs.first) >> bit_count) << bit_count;
  return {first_value, bit_count};
}
long long sum(long long cur) {
  long long u = cur, v = cur + 1;
  if (u & 1)
    v >>= 1;
  else
    u >>= 1;
  if (u >= mod) u %= mod;
  if (v >= mod) v %= mod;
  return u * v % mod;
}
long long sum(long long u, long long v) {
  long long res = sum(v) - sum(u - 1);
  if (res < 0) res += mod;
  return res;
}
long long get_nxt(pair<long long, int> cur) {
  return cur.first + (1ll << cur.second) - 1;
}
vector<pair<long long, int>> res;
void get_all(const vector<vector<pair<long long, int>>>& lhs,
             const vector<vector<pair<long long, int>>>& rhs) {
  for (auto& u : lhs) {
    for (auto& v : rhs) {
      for (auto& cur : v) {
        int i = 0;
        while (i + 1 < (int)(u).size() && u[i + 1].second < cur.second) ++i;
        res.push_back(get_transform_node(cur, u[i]));
        if (i + 1 < (int)(u).size())
          res.push_back(get_transform_node(cur, u[i + 1]));
        i = (int)(u).size() - 1;
        while (i - 1 > 0 && u[i - 1].second < cur.second) --i;
        res.push_back(get_transform_node(cur, u[i]));
        if (i - 1 >= 0) res.push_back(get_transform_node(cur, u[i - 1]));
      }
    }
  }
  return;
}
void solve() {
  auto up = get_nodes_from_input();
  auto down = get_nodes_from_input();
  get_all(up, down);
  get_all(down, up);
  sort(res.begin(), res.end());
  long long total = 0;
  for (int i = 0; i < (int)(res).size(); ++i) {
    long long cur = res[i].first;
    long long nxt = res[i].first + (1ll << res[i].second) - 1;
    while (i + 1 < res.size() && nxt >= res[i + 1].first) {
      ++i;
      update_max(nxt, res[i].first + (1ll << res[i].second) - 1);
    }
    total += sum(cur, nxt);
    if (total >= mod) total -= mod;
  }
  cout << total << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  const bool multiple_test = false;
  int test = 1;
  if (multiple_test) cin >> test;
  for (int i = 0; i < test; ++i) {
    solve();
  }
}
