#include <bits/stdc++.h>
using namespace std;
void __print(long long x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  long long f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
long long Power(long long n, long long x, long long mod) {
  long long ans = 1;
  while (x > 0) {
    if (x & 1) ans = (ans * n) % mod;
    n = (n * n) % mod;
    x = x >> 1;
  }
  return ans;
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return (a.second < b.second);
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long getSum(long long n) {
  long long sum = 0;
  while (n != 0) {
    sum = sum + n % 10;
    n = n / 10;
  }
  return sum;
}
long long myceil(long long n, long long x) {
  long long y;
  if (n % x == 0) {
    y = n / x;
    return y;
  } else {
    y = n / x + 1;
    return y;
  }
}
long long maxCountSameSUM(long long arr[], long long n) {
  unordered_map<long long, long long> M;
  for (long long i = 0; i < n - 1; i++)
    for (long long j = i + 1; j < n; j++) M[(arr[i] + arr[j])]++;
  long long max_count = 0;
  for (auto ele : M)
    if (max_count < ele.second) max_count = ele.second;
  return max_count;
}
long long md = 1000000007;
vector<long long> fact, inv;
void inverse(long long n) {
  if (n >= inv.size()) {
    long long size = inv.size();
    size = size == 0 ? 1 : size;
    inv.resize(n + 1);
    inv[0] = 1;
    for (long long i = size; i <= n; i++) inv[i] = Power(fact[i], md - 2, md);
  }
}
void factorial(long long n) {
  if (n >= fact.size()) {
    long long size = fact.size();
    size = size == 0 ? 1 : size;
    fact.resize(n + 1);
    fact[0] = 1;
    for (long long i = size; i <= n; i++) fact[i] = (fact[i - 1] * i) % md;
  }
}
long long ncr(long long n, long long r) {
  return (((fact[n] * inv[r]) % md) * inv[n - r]) % md;
}
vector<long long> visited;
queue<long long> q;
vector<long long> dist;
vector<long long> visited2;
vector<long long> ans;
vector<vector<pair<long long, long long>>> adj;
vector<long long> dp;
long long start = -1;
long long last = -1;
long long cnt = 0;
vector<long long> a;
void dfs(long long x, long long color) {
  if (visited[x]) return;
  visited[x] = 1;
  for (auto &&c : adj[x]) {
    if (c.second == color) dfs(c.first, color);
  }
}
void run_testcase() {
  long long n, m;
  cin >> n >> m;
  adj = vector<vector<pair<long long, long long>>>(n);
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    long long cnt = 0;
    for (long long j = 1; j <= 100; j++) {
      visited = vector<long long>(n);
      dfs(u, j);
      if (visited[v] == 1) {
        cnt++;
      }
    }
    cout << cnt << "\n";
  }
}
void q3() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  long long count = 0;
  long long sum1 = 0, sum2 = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  if (n == 1) {
    cout << -1 << "\n";
    return;
  } else {
    long long i = n - 1;
    while (i >= 0 && sum1 < k && sum2 < k) {
      sum1 += a[i];
      i--;
      count++;
      if (i >= 0) {
        sum2 += a[i];
        count++;
        i--;
      }
    }
    if (i >= 0) {
      if (sum1 >= k && sum2 >= k) {
        cout << count << "\n";
        return;
      }
      if (sum2 >= k) {
        while (i >= 0 && sum1 < k) {
          sum1 += a[i];
          count++;
          i--;
        }
        if (sum1 >= k) {
          cout << count << "\n";
        } else {
          cout << -1 << "\n";
        }
      } else if (sum1 >= k) {
        while (i >= 0 && sum2 < k) {
          sum2 += a[i];
          count++;
          i--;
        }
        if (sum2 >= k) {
          cout << count << "\n";
        } else {
          cout << -1 << "\n";
        }
      } else {
        cout << -1 << "\n";
      }
    } else {
      if (sum1 >= k && sum2 >= k) {
        cout << count << "\n";
      } else {
        cout << -1 << "\n";
      }
    }
  }
}
void q2() {}
void q4() {
  long long n;
  cin >> n;
}
int32_t main() {
  { run_testcase(); }
  return 0;
}
