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;
const int MOD = 998244353;
const int MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = 4 * atan((long double)1);
template <class T>
bool ckmin(T& a, const T& b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
namespace input {
template <class T>
void re(complex<T>& x);
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
  cin >> x;
}
void re(double& x) {
  string t;
  re(t);
  x = stod(t);
}
void re(long double& x) {
  string t;
  re(t);
  x = stold(t);
}
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
  re(t);
  re(ts...);
}
template <class T>
void re(complex<T>& x) {
  T a, b;
  re(a, b);
  x = cd(a, b);
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
  re(p.first, p.second);
}
template <class T>
void re(vector<T>& a) {
  for (int i = (0); i < ((int)a.size()); ++i) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
  for (int i = (0); i < (SZ); ++i) re(a[i]);
}
}  // namespace input
using namespace input;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(long long x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(long double x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
  cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
  pr(t);
  pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
  pr("{", x.first, ", ", x.second, "}");
}
template <class T>
void pr(const T& x) {
  pr("{");
  bool fst = 1;
  for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
  pr("}");
}
void ps() { pr("\n"); }
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
  pr(t);
  if (sizeof...(ts)) pr(" ");
  ps(ts...);
}
void pc() { pr("]\n"); }
template <class T, class... Ts>
void pc(const T& t, const Ts&... ts) {
  pr(t);
  if (sizeof...(ts)) pr(", ");
  pc(ts...);
}
}  // namespace output
using namespace output;
namespace io {
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void setIO(string second = "") {
  cin.sync_with_stdio(0);
  cin.tie(0);
  if ((int)second.size()) {
    setIn(second + ".in"), setOut(second + ".out");
  }
}
}  // namespace io
using namespace io;
template <class T>
struct modular {
  T val;
  explicit operator T() const { return val; }
  modular() { val = 0; }
  modular(const long long& v) {
    val = (-MOD <= v && v <= MOD) ? v : v % MOD;
    if (val < 0) val += MOD;
  }
  friend void pr(const modular& a) { pr(a.val); }
  friend void re(modular& a) {
    long long x;
    re(x);
    a = modular(x);
  }
  friend bool operator==(const modular& a, const modular& b) {
    return a.val == b.val;
  }
  friend bool operator!=(const modular& a, const modular& b) {
    return !(a == b);
  }
  friend bool operator<(const modular& a, const modular& b) {
    return a.val < b.val;
  }
  modular operator-() const { return modular(-val); }
  modular& operator+=(const modular& m) {
    if ((val += m.val) >= MOD) val -= MOD;
    return *this;
  }
  modular& operator-=(const modular& m) {
    if ((val -= m.val) < 0) val += MOD;
    return *this;
  }
  modular& operator*=(const modular& m) {
    val = (long long)val * m.val % MOD;
    return *this;
  }
  friend modular pow(modular a, long long p) {
    modular ans = 1;
    for (; p; p /= 2, a *= a)
      if (p & 1) ans *= a;
    return ans;
  }
  friend modular inv(const modular& a) {
    assert(a != 0);
    return pow(a, MOD - 2);
  }
  modular& operator/=(const modular& m) { return (*this) *= inv(m); }
  friend modular operator+(modular a, const modular& b) { return a += b; }
  friend modular operator-(modular a, const modular& b) { return a -= b; }
  friend modular operator*(modular a, const modular& b) { return a *= b; }
  friend modular operator/(modular a, const modular& b) { return a /= b; }
};
int na, nb;
vector<vector<pair<long long, int>>> a, b;
void dfs(vector<pair<long long, int>>& a, long long l, long long r, int d) {
  if (l > r) return;
  if (l % (1LL << (d + 1)) == 0 && r - l + 1 == (1LL << (d + 1))) {
    a.push_back({l, d + 1});
    return;
  }
  long long m = ((l + (1LL << d) - 1) >> d) << d;
  if (m == l) {
    if (r >= l + (1LL << d)) {
      dfs(a, l, l + (1LL << d) - 1, d - 1);
      dfs(a, l + (1LL << d), r, d - 1);
      return;
    }
  }
  if (m > r) {
    dfs(a, l, r, d - 1);
    return;
  }
  dfs(a, l, m - 1, d - 1);
  dfs(a, m, r, d - 1);
}
vector<pair<long long, int>> gen(long long l, long long r) {
  vector<pair<long long, int>> v;
  dfs(v, l, r, 59);
  return v;
}
vector<pair<long long, int>> zz, ZZ;
void insert(long long l, long long r) {
  ZZ.push_back({l, 1});
  ZZ.push_back({l + (1LL << r), -1});
}
void check() {
  for (auto& B : b) sort(begin(B), end(B));
  for (auto& A : a) {
    sort(begin(A), end(A), [](pair<long long, int> a, pair<long long, int> b) {
      return a.second > b.second;
    });
    ZZ.clear();
    for (auto& B : b) {
      int l = 0;
      while (l + 1 < (int)B.size() && B[l + 1].second > B[l].second) l++;
      int r = l + 1;
      for (auto& AA : A) {
        while (l >= 0 && B[l].second > AA.second) l--;
        while (r < (int)B.size() && B[r].second > AA.second) r++;
        vector<long long> v;
        if (l >= 0) {
          v.push_back((B[0].first ^ AA.first) >> AA.second << AA.second);
          v.push_back((B[l].first ^ AA.first) >> AA.second << AA.second);
        }
        if (r < (int)B.size()) {
          v.push_back((B[r].first ^ AA.first) >> AA.second << AA.second);
          v.push_back((B[(int)B.size() - 1].first ^ AA.first) >>
                      AA.second << AA.second);
        }
        sort(begin(v), end(v));
        for (int i = (0); i < ((int)v.size()); ++i) {
          if (i && v[i] == v[i - 1]) continue;
          insert(v[i], AA.second);
        }
      }
    }
    sort(begin(ZZ), end(ZZ));
    int sum = 0;
    for (auto& t : ZZ) {
      sum += t.second;
      if (t.second == -1 && sum == 0) zz.push_back(t);
      if (t.second == 1 && sum == 1) zz.push_back(t);
    }
  }
}
modular<int> i2 = (MOD + 1) / 2;
modular<int> ari(modular<int> l, modular<int> r) {
  return (r - l + 1) * (l + r) * i2;
}
long long ran() { return rand() % (1 << 30); }
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  srand(9);
  re(na);
  for (int i = (0); i < (na); ++i) {
    long long l, r;
    re(l, r);
    a.push_back(gen(l, r));
  }
  re(nb);
  for (int i = (0); i < (nb); ++i) {
    long long l, r;
    re(l, r);
    b.push_back(gen(l, r));
  }
  check();
  swap(a, b);
  check();
  sort(begin(zz), end(zz));
  int sum = 0;
  modular<int> ans = 0;
  for (int i = (0); i < ((int)zz.size() - 1); ++i) {
    sum += zz[i].second;
    if (sum) ans += ari(zz[i].first, zz[i + 1].first - 1);
  }
  ps(ans);
}
